I have a question about the Reduce function: x <- list() x[[1]] <- LETTERS[1:5] x[[2]] <- LETTERS[11:15] Reduce(paste, x) [1] "A K" "B L" "C M" "D N" "E O" How do I get this?: [1] "A" "K" [2] "B" "L" [3] "C" "M" [4] "D" "N" [5] "E" "O" Thanks for your help! -- View this message in context: http://r.789695.n4.nabble.com/Reduce-paste-x-question-tp4648151.html Sent from the R help mailing list archive at Nabble.com.
What is "this"? A matrix with two columns? A data frame? You did an excellent job of providing data to reproduce your attempt so far; thank you. But I'm not sure what you want as result. But maybe one of these two things?> data.frame(x, stringsAsFactors=FALSE)c..A....B....C....D....E.. c..K....L....M....N....O.. 1 A K 2 B L 3 C M 4 D N 5 E O> matrix(unlist(x), ncol=2)[,1] [,2] [1,] "A" "K" [2,] "B" "L" [3,] "C" "M" [4,] "D" "N" [5,] "E" "O" On Thu, Nov 1, 2012 at 2:47 PM, mdvaan <mathijsdevaan at gmail.com> wrote:> I have a question about the Reduce function: > > x <- list() > x[[1]] <- LETTERS[1:5] > x[[2]] <- LETTERS[11:15] > Reduce(paste, x) > [1] "A K" "B L" "C M" "D N" "E O" > > How do I get this?: > [1] "A" "K" > [2] "B" "L" > [3] "C" "M" > [4] "D" "N" > [5] "E" "O" > > Thanks for your help! > >-- Sarah Goslee http://www.functionaldiversity.org
On Thu, Nov 1, 2012 at 6:47 PM, mdvaan <mathijsdevaan at gmail.com> wrote:> I have a question about the Reduce function: > > x <- list() > x[[1]] <- LETTERS[1:5] > x[[2]] <- LETTERS[11:15] > Reduce(paste, x) > [1] "A K" "B L" "C M" "D N" "E O" > > How do I get this?: > [1] "A" "K" > [2] "B" "L" > [3] "C" "M" > [4] "D" "N" > [5] "E" "O" >Can you be specific as to what that output actually is? (Use dput() if you don't mind) I'm not entirely sure what sort of R data structure gives a printout like that by default. Michael
HI, Try this: I am not sure about the use of paste() here. To get the output from x: ?? sapply(x,function(x) x) ???? [,1] [,2] [1,] "A"? "K" [2,] "B"? "L" [3,] "C"? "M" [4,] "D"? "N" [5,] "E"? "O" If you want to get the result from: ?res<-Reduce(paste, x) ?do.call(rbind,strsplit(res," ")) #???? [,1] [,2] #[1,] "A"? "K" #[2,] "B"? "L" #[3,] "C"? "M" #[4,] "D"? "N" #[5,] "E"? "O" A.K. ----- Original Message ----- From: mdvaan <mathijsdevaan at gmail.com> To: r-help at r-project.org Cc: Sent: Thursday, November 1, 2012 2:47 PM Subject: [R] Reduce(paste, x) question I have a question about the Reduce function: x <- list() x[[1]] <- LETTERS[1:5] x[[2]] <- LETTERS[11:15] Reduce(paste, x) [1] "A K" "B L" "C M" "D N" "E O" How do I get this?: [1] "A" "K" [2] "B" "L" [3] "C" "M" [4] "D" "N" [5] "E" "O" Thanks for your help! -- View this message in context: http://r.789695.n4.nabble.com/Reduce-paste-x-question-tp4648151.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
It is not clear exactly what you want. Do you want the final object to be a matrix with 2 columns (or the number of columns equal to the number of items in the list? If so you could just use do.call(cbind, x) and you would not need reduce or paste (paste is the wrong tool unless you want a single string with the different things pasted into it). Do you want a list with each element of the list being a vector? Do you want a string, but with quotes inside the string? Something else? On Thu, Nov 1, 2012 at 12:47 PM, mdvaan <mathijsdevaan at gmail.com> wrote:> I have a question about the Reduce function: > > x <- list() > x[[1]] <- LETTERS[1:5] > x[[2]] <- LETTERS[11:15] > Reduce(paste, x) > [1] "A K" "B L" "C M" "D N" "E O" > > How do I get this?: > [1] "A" "K" > [2] "B" "L" > [3] "C" "M" > [4] "D" "N" > [5] "E" "O" > > Thanks for your help! > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Reduce-paste-x-question-tp4648151.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com
I should have been more specific: y <- list() a <- c("A", "K") b <- c("B", "L") c <- c("C", "M") d <- c("D", "N") e <- c("E", "O") y[[1]] <- a y[[2]] <- b y[[3]] <- c y[[4]] <- d y[[5]] <- e y [[1]] [1] "A" "K" [[2]] [1] "B" "L" [[3]] [1] "C" "M" [[4]] [1] "D" "N" [[5]] [1] "E" "O" How do I get a list object like y (each element of y is a vector of strings) from: x[[1]] <- LETTERS[1:5] x[[2]] <- LETTERS[11:15] using only the Reduce function? Thanks! -- View this message in context: http://r.789695.n4.nabble.com/Reduce-paste-x-question-tp4648151p4648169.html Sent from the R help mailing list archive at Nabble.com.
Hi, This is still a combination. ?split(Reduce(cbind,x),1:nrow(Reduce(cbind,x))) #$`1` #[1] "A" "K" # #$`2` #[1] "B" "L" # #$`3` #[1] "C" "M" # #$`4` #[1] "D" "N" # #$`5` #[1] "E" "O" A.K. ________________________________ From: Mathijs de Vaan <mathijsdevaan at gmail.com> To: arun <smartpink111 at yahoo.com> Cc: R help <r-help at r-project.org> Sent: Thursday, November 1, 2012 4:00 PM Subject: Re: [R] Reduce(paste, x) question I should have been more specific: y <- list() a <- c("A", "K") b <- c("B", "L") c <-?c("C", "M") d?<-?c("D", "N") e?<-?c("E", "O") y[[1]] <- a y[[2]] <- b y[[3]] <- c y[[4]] <- d y[[5]] <- e y [[1]] [1] "A" "K" [[2]] [1] "B" "L" [[3]] [1] "C" "M" [[4]] [1] "D" "N" [[5]] [1] "E" "O" How do I get a list object like y (each element of y is a vector of strings) from: x[[1]] <- LETTERS[1:5] x[[2]] <- LETTERS[11:15] using only the Reduce function? Thanks! On Thu, Nov 1, 2012 at 3:43 PM, arun <smartpink111 at yahoo.com> wrote: HI,> >Try this: >I am not sure about the use of paste() here. >To get the output from x: ?? >sapply(x,function(x) x) > >???? [,1] [,2] >[1,] "A"? "K" >[2,] "B"? "L" >[3,] "C"? "M" >[4,] "D"? "N" >[5,] "E"? "O" > >If you want to get the result from: >?res<-Reduce(paste, x) >?do.call(rbind,strsplit(res," ")) >#???? [,1] [,2] >#[1,] "A"? "K" >#[2,] "B"? "L" >#[3,] "C"? "M" >#[4,] "D"? "N" >#[5,] "E"? "O" > > >A.K. > > > > > >----- Original Message ----- >From: mdvaan <mathijsdevaan at gmail.com> >To: r-help at r-project.org >Cc: >Sent: Thursday, November 1, 2012 2:47 PM >Subject: [R] Reduce(paste, x) question > >I have a question about the Reduce function: > >x <- list() >x[[1]] <- LETTERS[1:5] >x[[2]] <- LETTERS[11:15] >Reduce(paste, x) >[1] "A K" "B L" "C M" "D N" "E O" > >How do I get this?: >[1] "A" "K" >[2] "B" "L" >[3] "C" "M" >[4] "D" "N" >[5] "E" "O" > > >Thanks for your help! > > > >-- >View this message in context: http://r.789695.n4.nabble.com/Reduce-paste-x-question-tp4648151.html >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >R-help at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. > >