Hello all I am new in R and so easy stuff are difficult... let say that I have a list test <- list(a=c("x","v"),b=c("n","m")) how can I without a loop get test$a bind with test$b (obviously in real life their would be many fields) Cheers and thanks -- View this message in context: http://n4.nabble.com/Bind-field-of-a-list-tp1561676p1561676.html Sent from the R help mailing list archive at Nabble.com.
is this what you're looking for? test <- data.frame(a=c("x","v"),b=c("n","m")) test statquant wrote:> Hello all > I am new in R and so easy stuff are difficult... > let say that I have a list > test <- list(a=c("x","v"),b=c("n","m")) > how can I without a loop get test$a bind with test$b (obviously in real life > their would be many fields) > > Cheers and thanks
Hello, Thank you but I think not what I would like to get as an answer is the list ("x","v","n","m") + what you gave me could work for 2 fields but if I have 200... What I want is a vectorize way to do bindlists <- function(x){ output = c(); for (i in 1:length(x)) { output = c(output,x[[i]]) } return(output) } Regards -- View this message in context: http://n4.nabble.com/Bind-field-of-a-list-tp1561676p1561727.html Sent from the R help mailing list archive at Nabble.com.
Hi, Try this, unlist(test) or do.call(c, test) HTH, baptiste On 19 February 2010 15:19, statquant <colin.umansky at barcap.com> wrote:> > Hello all > I am new in R and so easy stuff are difficult... > let say that I have a list > test <- list(a=c("x","v"),b=c("n","m")) > how can I without a loop get test$a bind with test$b (obviously in real life > their would be many fields) > > Cheers and thanks > -- > View this message in context: http://n4.nabble.com/Bind-field-of-a-list-tp1561676p1561676.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. >
Bravo baptiste it works what does do.call do exactly ?? -- View this message in context: http://n4.nabble.com/Bind-field-of-a-list-tp1561676p1561745.html Sent from the R help mailing list archive at Nabble.com.
On Feb 19, 2010, at 9:49 AM, statquant wrote:> > Hello, > Thank you but I think not what I would like to get as an answer is > the list > ("x","v","n","m") + what you gave me could work for 2 fields but if > I have > 200... > > > What I want is a vectorize way to do > bindlists <- function(x){ > output = c(); > for (i in 1:length(x)) > { > output = c(output,x[[i]]) > } > return(output) > }Instead try: c( sapply(test, "[") ) The inner (implicit) loop gets you a matrix and the outer c() turns it into the requested vector. You could have coerced the matrix to vector with as.vector, too.> > Regards > -- > View this message in context: http://n4.nabble.com/Bind-field-of-a-list-tp1561676p1561727.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.David Winsemius, MD Heritage Laboratories West Hartford, CT