[R] Converting a list to a data frame

David Winsemius dwin@emiu@ @ending from comc@@t@net
Thu May 3 18:32:14 CEST 2018


> On May 2, 2018, at 2:43 PM, David L Carlson <dcarlson at tamu.edu> wrote:
> 
> Typo: dat[[z]] should be x[[z]]:
> 
> x2 <- do.call(rbind, lapply(names(x), function(z) 
>      data.frame(type=z, x[[z]])))
> x2
>  type x y
> 1    A 1 3
> 2    A 2 4
> 3    B 5 7
> 4    B 6 8

Sometimes one want to "bind" list that have some columns in common and others that you want to include from one but leave as NA for the rows from the other. The rbind.list function from package-plyr is from the pre-tidy/tibble era of Hadley's efforts and provides that facility out of the box. It also seems to "work" with the do.call(rbind strategy for named lists above.

x <- list(A=mtcars[c("mpg", "wt")] , B=mtcars[c("wt", "cyl")])  #mtcars
x2 <- do.call(rbind.fill, lapply(names(x), function(z) 
     data.frame(type=z, x[[z]])))
str(x2)
#---------
'data.frame':	64 obs. of  4 variables:
 type: Factor w/ 2 levels "A","B": 1 1 1 1 1 1 1 1 1 1 ...
 mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 cyl : num  NA NA NA NA NA NA NA NA NA NA ...

-- 
David, the other.

> 
> David C
> 
> -----Original Message-----
> From: R-help <r-help-bounces at r-project.org> On Behalf Of David L Carlson
> Sent: Wednesday, May 2, 2018 3:51 PM
> To: William Dunlap <wdunlap at tibco.com>; Kevin E. Thorpe <kevin.thorpe at utoronto.ca>
> Cc: r-help mailing list <r-help at r-project.org>
> Subject: Re: [R] Converting a list to a data frame
> 
> Or add the type column first and then rbind:
> 
> x <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
> x2 <- do.call(rbind, lapply(names(x), function(z) 
>      data.frame(type=z, dat[[z]])))
> 
> ----------------------------------------
> David L Carlson
> Department of Anthropology
> Texas A&M University
> College Station, TX 77843-4352
> 
> -----Original Message-----
> From: R-help <r-help-bounces at r-project.org> On Behalf Of William Dunlap via R-help
> Sent: Wednesday, May 2, 2018 12:28 PM
> To: Kevin E. Thorpe <kevin.thorpe at utoronto.ca>
> Cc: R Help Mailing List <r-help at r-project.org>
> Subject: Re: [R] Converting a list to a data frame
> 
>> x1 <- do.call(rbind, c(x, list(make.row.names=FALSE)))
>> x2 <- cbind(type=rep(names(x), vapply(x, nrow, 0)), x1)
>> str(x2)
> 'data.frame':   4 obs. of  3 variables:
> $ type: Factor w/ 2 levels "A","B": 1 1 2 2
> $ x   : int  1 2 5 6
> $ y   : int  3 4 7 8
> 
> 
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
> 
> On Wed, May 2, 2018 at 10:11 AM, Kevin E. Thorpe <kevin.thorpe at utoronto.ca>
> wrote:
> 
>> I suspect this is pretty easy, but I'm having trouble figuring it out.
>> Basically, I have a list of data frames such as the following example:
>> 
>> list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
>> 
>> I would like to turn this into  data frame where the list elements are 
>> essentially rbind'ed together and the element name becomes a new variable.
>> For example, I would like to turn the list above into a data frame 
>> that looks like this:
>> 
>> data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8))
>> 
>> Appreciate any pointers.
>> 
>> Kevin
>> 
>> --
>> Kevin E. Thorpe
>> Head of Biostatistics,  Applied Health Research Centre (AHRC) Li Ka 
>> Shing Knowledge Institute of St. Michael's Hospital Assistant 
>> Professor, Dalla Lana School of Public Health University of Toronto
>> email: kevin.thorpe at utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posti 
>> ng-guide.html and provide commented, minimal, self-contained, 
>> reproducible code.
>> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law




More information about the R-help mailing list