[R] binding two lists of lists of dataframes together

Vin Cheng newrnewbie at hotmail.com
Tue May 12 18:24:14 CEST 2015


Hi,
  
 Apologies for the newbie error.  Thanks John, Jim, Jeff!
  
 Please find the dput output below.  I'm trying to take list1 and list 2 and bind them together at an individual list level by column - all the structures and col names will be the same, but the number of rows could be different for any sublist.  The output should end up looking like list3.  
  
 Please let me know if this remains unclear.  Any help/guidance would be greatly appreciated!!
  
 Many Thanks,
 Vince
  
  
 list1<-list(structure(list(id = c(493L, 564L, 147L, 83L, 33L, 276L, 402L, 285L, 30L, 555L), 
                     WgtBand = c(1, 1, 2, 3, 4, 5, 6, 7, 8, 9), 
                     Wgt = c(NaN, NaN, NA, NA, NA, NA, NA, NA, NA, NA)), 
                .Names = c("id", "WgtBand", "Wgt")), 
      structure(list(id = c(76L, 330L, 574L, 47L, 131L, 581L, 133L, 69L, 35L, 487L), 
                     WgtBand = c(1, 1, 2, 3, 4,5, 6, 7, 8, 9), 
                     Wgt = c(NaN, NaN, NA, NA, NA, NA, NA, NA, NA, NA)), 
                .Names = c("id", "WgtBand", "Wgt")), 
      structure(list(id = c(376L, 130L, 574L, 47L, 131L, 581L, 133L, 69L, 35L, 487L), 
                     WgtBand = c(1, 1, 2, 3, 4,5, 6, 7, 8, 9), 
                     Wgt = c(NaN, NaN, NA, NA, NA, NA, NA, NA, NA, NA)), 
                .Names = c("id", "WgtBand", "Wgt")))


 list2<-list(structure(list(id = c(493L, 564L, 147L), 
                            WgtBand = c(1, 2, 3), 
                            Wgt = c(NaN, NaN, NA)), 
                       .Names = c("id", "WgtBand", "Wgt")), 
             structure(list(id = c(276L, 411L, 574L,111L), 
                            WgtBand = c(1, 2, 3,4), 
                            Wgt = c(NaN, NaN, NA,NA)), 
                       .Names = c("id", "WgtBand", "Wgt")), 
             structure(list(id = c(76L, 330L), 
                            WgtBand = c(1, 1), 
                            Wgt = c(NaN, NaN)), 
                       .Names = c("id", "WgtBand", "Wgt")))

 list3<-list(structure(list(id = c(493L, 564L, 147L, 83L, 33L, 276L, 402L, 285L, 30L, 555L,493L, 564L, 147L), 
                            WgtBand = c(1, 1, 2, 3, 4, 5, 6, 7, 8, 9,1, 2, 3), 
                            Wgt = c(NaN, NaN, NA, NA, NA, NA, NA, NA, NA, NA,NaN, NaN, NA)), 
                       .Names = c("id", "WgtBand", "Wgt")), 
             structure(list(id = c(376L, 130L, 574L, 47L, 131L, 581L, 133L, 69L, 35L, 487L,276L, 411L, 574L,111L), 
                            WgtBand = c(1, 1, 2, 3, 4,5, 6, 7, 8, 9, 1, 2, 3,4), 
                            Wgt = c(NaN, NaN, NA, NA, NA, NA, NA, NA, NA, NA,NaN, NaN, NA,NA)), 
                       .Names = c("id", "WgtBand", "Wgt")), 
             structure(list(id = c(76L, 330L, 574L, 47L, 131L, 581L, 133L, 69L, 35L, 487L,76L, 330L), 
                            WgtBand = c(1, 1, 2, 3, 4,5, 6, 7, 8, 9, 1, 1), 
                            Wgt = c(NaN, NaN, NA, NA, NA, NA, NA, NA, NA, NA,NaN, NaN)), 
                       .Names = c("id", "WgtBand", "Wgt")))
 
> Date: Mon, 11 May 2015 18:19:06 -0800
> From: jrkrideau at inbox.com
> Subject: RE: [R] binding two lists of lists of dataframes together
> To: newrnewbie at hotmail.com; r-help at r-project.org
> 
>  http://adv-r.had.co.nz/Reproducibility.html
> 
> and please do not post in Html.
> 
> 
> John Kane
> Kingston ON Canada
> 
> 
> > -----Original Message-----
> > From: newrnewbie at hotmail.com
> > Sent: Mon, 11 May 2015 20:16:40 +0000
> > To: r-help at r-project.org
> > Subject: [R] binding two lists of lists of dataframes together
> > 
> >  Hi,
> > I'm new to R and am stumped.  I'm trying to bind List 1 to List 2 and
> > have the corresponding Output.
> > 
> > I've found the following code - I can't say I understand
> > rbindlist(lapply(list12, "[", i, TRUE)).  Either way - it doesn't give
> > exactly what's needed.
> > 
> > library(data.table)
> > list12 <- list(List1,List2)
> > nr <- as.vector(nrow(list12[[1]]))
> > fastbind.ith.rows <- function(i) rbindlist(lapply(list12, "[", i, TRUE))
> > fastbound <- lapply(1:nr, fastbind.ith.rows)
> > 
> > It produces Output 2 - where dataframes are grouped together by rownames,
> > but keeps 2 separate vectors - vs. binding the two into 1 vector.
> > 
> > Any help/guidance would be greatly appreciated!!
> > 
> > Thanks!
> > Vince
 		 	   		  
	[[alternative HTML version deleted]]



More information about the R-help mailing list