Hello, is there an elegant way, how I can convert each row of a data frame into distinct elements of a list? In essence, what I'm looking for is something like rows.to.lists <- function( df ) { ll <- NULL for( i in 1:nrow(df) ) ll <- append( ll, list(df[i,]) ) return (ll) } but more done more efficiently (the data frame may contain ten-thousands of rows). I thought about using apply() but this function always returns a matrix. Thanks in advance! Bye, Sebastian
> as.data.frame(t(df))For example> x <- as.data.frame(t(mtcars)) > typeof(x)[1] "list" -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Sebastian Bauer Sent: Tuesday, March 02, 2010 8:12 AM To: r-help at r-project.org Subject: [R] Row-wisely converting a data frame into a list Hello, is there an elegant way, how I can convert each row of a data frame into distinct elements of a list? In essence, what I'm looking for is something like rows.to.lists <- function( df ) { ll <- NULL for( i in 1:nrow(df) ) ll <- append( ll, list(df[i,]) ) return (ll) } but more done more efficiently (the data frame may contain ten-thousands of rows). I thought about using apply() but this function always returns a matrix. Thanks in advance! Bye, Sebastian ______________________________________________ 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. ================================== P Please consider the environment before printing this e-mail Cleveland Clinic is ranked one of the top hospitals in America by U.S.News & World Report (2009). Visit us online at http://www.clevelandclinic.org for a complete listing of our services, staff and locations. Confidentiality Note: This message is intended for use\...{{dropped:13}}
On Mar 2, 2010, at 8:11 AM, Sebastian Bauer wrote:> Hello, > > is there an elegant way, how I can convert each row of a data frame > into distinct elements of a list?split(dfrm, rownames(dfrm))> > In essence, what I'm looking for is something like > > rows.to.lists <- function( df ) { > ll <- NULL > for( i in 1:nrow(df) ) > ll <- append( ll, list(df[i,]) ) > return (ll) > } > > but more done more efficiently (the data frame may contain ten- > thousands of rows). I thought about using apply() but this function > always returns a matrix. > > Thanks in advance! > > Bye, > Sebastian > > ______________________________________________ > 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.