[R] Converting "list of data frame" to data frame

jim holtman jholtman at gmail.com
Mon Apr 30 17:53:35 CEST 2007


This does not look like a "list of dataframes" where each dataframe is
an element of the list.  This appears to just be a list with all the
elements of the data frames at the same levels.  A list of data frames
would look like this:

> x <- list(data.frame(a=1:5, b=1:5), data.frame(a=6:10, b=6:10))

> str(x)
List of 2
 $ :'data.frame':       5 obs. of  2 variables:
  ..$ a: int [1:5] 1 2 3 4 5
  ..$ b: int [1:5] 1 2 3 4 5
 $ :'data.frame':       5 obs. of  2 variables:
  ..$ a: int [1:5] 6 7 8 9 10
  ..$ b: int [1:5] 6 7 8 9 10
>

It would appear that if you have one large list, then you will have to
individually split off each of the 10 elements (this appears to be the
pattern) into a data frame and then 'rbind' the data.


On 4/30/07, Ajit Pawar <ajitpawar75 at gmail.com> wrote:
> Greetings Douglas/Jim/John/R-help,
>          Thanks for your help so far.
>
>           Answering your questions - doing an "str' on the list reveals
> following (head) information  :
>
> ---------------
>  $ : Factor w/ 729 levels "XX1","YY1",..: 6 9 10 12 13 14 19 22 29 30 ...
>  $ : int [1:109] 19950201 19950201 19950201 19950201 19950201 19950201
> 19950201 19950201 19950201 19950201 ...
>  $ : num [1:109] -0.01046 -0.00528 -0.02559  0.02216  0.00847 ...
>  $ : int [1:109] -133822 -351764 -7525444 127357 572325 -68655 -22455
> -1241321 298967 -208658 ...
>  $ : num [1:109]  -523244  -263823 -1279325  1108006   423520 ...
>  $ : int [1:109] 19950301 19950301 19950301 19950301 19950301 19950301
> 19950301 19950301 19950301 19950301 ...
>  $ : int [1:109] 2 4 5 6 13 10 4 3 7 9 ...
>  $ : num [1:109] 3.93 0.74 0.17 9.28 0.7 8.45 1.3 0.66 4.76 1.35 ...
>  $ : num [1:109] 0.001536 0.000246 0.000107 0.000755 0.000541 ...
>  $ : num [1:109]  0.0051 -0.0133  0.0000  0.0667 -0.0541 ...
>  $ : Factor w/ 729 levels "XX1","YY1",..: 4 5 6 9 10 11 12 13 14 16 ...
>  $ : int [1:196] 19950301 19950301 19950301 19950301 19950301 19950301
> 19950301 19950301 19950301 19950301 ...
>  $ : num [1:196]  0.00424  0.02575 -0.01155 -0.00540 -0.00272 ...
>  $ : int [1:196] 152489 146440 -152062 -377393 -828313 220702 107320 572325
> -156050 -298177 ...
>  $ : num [1:196]  219584 1332604 -597604 -279271 -140813 ...
>  $ : int [1:196] 19950401 19950401 19950401 19950401 NA 19950401 19950401
> 19950401 19950401 19950401 ...
>  $ : int [1:196] 2 7 2 4 NA 4 6 13 10 1 ...
>  $ : num [1:196] 1.38 9.08 3.55 0.91 NA 1.57 11 0.54 8.35 3.48 ...
>  $ : num [1:196] 0.000801 0.003298 0.001468 0.000303       NA ...
>  $ : num [1:196] -0.0069 -0.0022 -0.0967  0.2297      NA ...
>  $ : Factor w/ 729 levels "XX1","YY1",..: 4 5 6 9 11 12 13 14 16 17 ...
>
> ---------------
> Just typing the list name reveals the following information:
>
>      [,1]        [,2]        [,3]        [,4]        [,5]        [,6]
> idfactor,109  factor,196  factor,199  factor,181  factor,183  factor,181
> info1  Integer,109 Integer,196 Integer,199 Integer,181 Integer,183
> Integer,181
> info2 Numeric,109 Numeric,196 Numeric,199 Numeric,181 Numeric,183
> Numeric,181
> info3  Integer,109 Integer,196 Integer,199 Integer,181 Integer,183
> Integer,181
> info4  Numeric,109 Numeric,196 Numeric,199 Numeric,181 Numeric,183
> Numeric,181
> info5  Integer,109 Integer,196 Integer,199 Integer,181 Integer,183
> Integer,181
> ..
> ....
> ...
> info10  Integer,109 Integer,196 Integer,199 Integer,181 Integer,183
> Integer,181
>
>  ---------------
> and dim & typeof information  shows the following:
>
> dim => [1]  10 140
>
> typeof => [1] "list"
>
> ---------------------
>
>        Does the above information help?
>
>
> Thanks a bunch!
>
> AP.
>
>
>
> On 4/30/07, John Kane <jrkrideau at yahoo.ca> wrote:
> >
> > I just tried an artifical example and the approach
> > seems to work okay
> >
> > # Joining data.frames stored in a list
> > # From "Douglas Bates"
> >
> > aa <- 1:4
> > bb <- 2:5
> > cc <- 3:6
> > dd <- 4:7
> > ee <- 5:8
> > ff <- c(rep(NA,4))
> > gg <- 6:9
> >
> > lst <- list(data.frame(aa,bb), data.frame(cc,dd),
> > data.frame(ee,ff,gg))
> > tatiana <- names(lst)  <-  c("A", "C", "E")
> >
> > maxy  <- do.call("rbind", c(lst$A, lst$C, lst$E)) ;
> > maxy
> >
> > miny  <- do.call("cbind", c(lst$A, lst$C, lst$E)) ;
> > miny
> >
> > What are the dimensions of your data frames?
> > --- Ajit Pawar <ajitpawar75 at gmail.com> wrote:
> >
> > > Douglas/R-help,
> > >         Thanks for your reply. I did try the
> > > solution but the result is not
> > > what I expect and I also get the following warning
> > > message:
> > >
> > > -------------------
> > >  Warning message:
> > >  number of columns of result
> > >         is not a multiple of vector length (arg 1)
> > > in: rbind(1, c(6, 9, 10,
> > > 12, 13, 14, 19, 22, 29, 30, 42, 45, 47,
> > >  -------------------
> > >
> > >         The "list of data frames" that sapply
> > > returns has same number of
> > > columns *but* different number of rows depending on
> > > the index of sapply.
> > >
> > >         Any idea what might be going wrong?
> > >
> > >         Many thanks in advance!.
> > >
> > > Cheers
> > >
> > > AP
> > >
> > >
> > >
> > >
> > >
> > >
> > > On 4/29/07, Douglas Bates <bates at stat.wisc.edu>
> > > wrote:
> > > >
> > > > On 4/28/07, Ajit Pawar <ajitpawar75 at gmail.com>
> > > wrote:
> > > > > Greetings,
> > > > >       This might be something very simple but a
> > > nice solution eludes
> > > > me!!
> > > > >
> > > > >        I have a function that I call within
> > > sapply that generates data
> > > > frame
> > > > > in each call. Now when sapply returns me back
> > > the result - it's in the
> > > > form
> > > > > of a "list of data frames". so in order to
> > > extract the information into
> > > > a
> > > > > single data frame I have to loop thru the
> > > following code:
> > > > >
> > > > >        for(i=1:n) {
> > > > >            my.df =
> > > rbind(my.df,list.from.sapply[,i]);
> > > > >        }
> > > > >
> > > > >    Is there anyway to code it better?
> > > >
> > > > do.call("rbind", my.df.list.from.sapply)
> > > >
> > >
> > >       [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > R-help at stat.math.ethz.ch 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.
> > >
> >
> >
> >
> >      Be smarter than spam. See how smart SpamGuard is at giving junk email
> > the boot with the All-new Yahoo! Mail at
> > http://mrd.mail.yahoo.com/try_beta?.intl=ca
> >
> >
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list