Skip to content
Prev 374408 / 398513 Next

Converting a list to a data frame

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

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
'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:
______________________________________________
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.