Another approach: ######## library(tidyr) L <- list( A = data.frame( x=1:2, y=3:4 ) , B = data.frame( x=5:6, y=7:8 ) ) D <- data.frame( Type = names( L ) , stringsAsFactors = FALSE ) D$data <- L unnest(D, data) #> Type x y #> 1 A 1 3 #> 2 A 2 4 #> 3 B 5 7 #> 4 B 6 8 ######## On Wed, 2 May 2018, Eivind K. Dovik wrote:> On Wed, 2 May 2018, Kevin E. Thorpe 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 > > Hi, Kevin. > > Here's code that will generate your desired data frame. > > # List as provided > thelist <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8)) > thelist > > # Creating the type-vector > type <- c() > for(i in 1:length(thelist)){ > type <- c(type, rep(names(thelist)[i], sapply(thelist, nrow)[i])) > } > > # Creating the data frame > df <- data.frame(type, do.call(rbind.data.frame, c(thelist, make.row.names = > FALSE))) > df > > > Kind regards, > Eivind K. Dovik > Bergen, NO > > > > >> >> -- >> 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 >> stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> 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 > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >--------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k
On Wed, May 2, 2018 at 11:53 AM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:> Another approach: > > ######## > library(tidyr) > L <- list( A = data.frame( x=1:2, y=3:4 ) > , B = data.frame( x=5:6, y=7:8 ) > ) > D <- data.frame( Type = names( L ) > , stringsAsFactors = FALSE > ) > D$data <- L > unnest(D, data) > #> Type x y > #> 1 A 1 3 > #> 2 A 2 4 > #> 3 B 5 7 > #> 4 B 6 8 > ########I think a slightly more idiomatic tidyverse solution is dplyr::bind_rows() l <- list( A = data.frame(x = 1:2, y = 3:4), B = data.frame(x = 5:6, y = 7:8) ) dplyr::bind_rows(l, .id = "type") #> type x y #> 1 A 1 3 #> 2 A 2 4 #> 3 B 5 7 #> 4 B 6 8 This also has the advantage of returning a data frame when the inputs are data frames. Hadley -- hadley.nz
On 05/03/2018 01:28 PM, Hadley Wickham wrote:> On Wed, May 2, 2018 at 11:53 AM, Jeff Newmiller > <jdnewmil at dcn.davis.ca.us> wrote: >> Another approach: >> >> ######## >> library(tidyr) >> L <- list( A = data.frame( x=1:2, y=3:4 ) >> , B = data.frame( x=5:6, y=7:8 ) >> ) >> D <- data.frame( Type = names( L ) >> , stringsAsFactors = FALSE >> ) >> D$data <- L >> unnest(D, data) >> #> Type x y >> #> 1 A 1 3 >> #> 2 A 2 4 >> #> 3 B 5 7 >> #> 4 B 6 8 >> ######## > > I think a slightly more idiomatic tidyverse solution is dplyr::bind_rows() > > l <- list( > A = data.frame(x = 1:2, y = 3:4), > B = data.frame(x = 5:6, y = 7:8) > ) > > dplyr::bind_rows(l, .id = "type") > #> type x y > #> 1 A 1 3 > #> 2 A 2 4 > #> 3 B 5 7 > #> 4 B 6 8 > > This also has the advantage of returning a data frame when the inputs > are data frames. > > Hadley >I _clearly_ need to learn the dplyr package. -- 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
If you require that the 'type' column be a factor with a level for each element of the input list, then you need to do that after calling dplyr::bind_rows(), just as with the base-R solutions.> l <- list( A = data.frame(X=1:2, Y=11:12), B = data.frame(X=integer(),Y=integer()), C = data.frame(X=3L, Y=13L) )> str(d <- dplyr::bind_rows(l, .id = "Which") )'data.frame': 3 obs. of 3 variables: $ Which: chr "A" "A" "C" $ X : int 1 2 3 $ Y : int 11 12 13> d$Which <- factor(d$Which, levels=names(l)) > str(d)'data.frame': 3 obs. of 3 variables: $ Which: Factor w/ 3 levels "A","B","C": 1 1 3 $ X : int 1 2 3 $ Y : int 11 12 13 Sometimes you need the names of the of the 0-row data.frames in the output to make plots, etc., comparable across various samples of the data. Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, May 3, 2018 at 10:28 AM, Hadley Wickham <h.wickham at gmail.com> wrote:> On Wed, May 2, 2018 at 11:53 AM, Jeff Newmiller > <jdnewmil at dcn.davis.ca.us> wrote: > > Another approach: > > > > ######## > > library(tidyr) > > L <- list( A = data.frame( x=1:2, y=3:4 ) > > , B = data.frame( x=5:6, y=7:8 ) > > ) > > D <- data.frame( Type = names( L ) > > , stringsAsFactors = FALSE > > ) > > D$data <- L > > unnest(D, data) > > #> Type x y > > #> 1 A 1 3 > > #> 2 A 2 4 > > #> 3 B 5 7 > > #> 4 B 6 8 > > ######## > > I think a slightly more idiomatic tidyverse solution is dplyr::bind_rows() > > l <- list( > A = data.frame(x = 1:2, y = 3:4), > B = data.frame(x = 5:6, y = 7:8) > ) > > dplyr::bind_rows(l, .id = "type") > #> type x y > #> 1 A 1 3 > #> 2 A 2 4 > #> 3 B 5 7 > #> 4 B 6 8 > > This also has the advantage of returning a data frame when the inputs > are data frames. > > Hadley > > -- > hadley.nz > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org > posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Maybe Matching Threads
- Converting a list to a data frame
- tidyverse repeating error: "object 'rlang_mut_env_parent' not found"
- splitting a dataframe in R based on multiple gene names in a specific column
- Simple Stacking of Two Columns
- tidyverse repeating error: "object 'rlang_mut_env_parent' not found"