[R] Converting "list of data frame" to data frame
jim holtman
jholtman at gmail.com
Sun Apr 29 15:07:16 CEST 2007
since you didn't supply a reproducible example, here is a test that I
ran with varying number of rows that seems to work fine. You might
want to provide at leat an "str" of the structure that you are doing
the rbind on.
> x <- lapply(1:10, function(z){
+ data.frame(a=runif(z), b=rnorm(z), c=factor(seq(z)))
+ })
> str(x)
List of 10
$ :'data.frame': 1 obs. of 3 variables:
..$ a: num 0.986
..$ b: num -0.543
..$ c: Factor w/ 1 level "1": 1
$ :'data.frame': 2 obs. of 3 variables:
..$ a: num [1:2] 0.8121 0.0772
..$ b: num [1:2] -0.349 -1.008
..$ c: Factor w/ 2 levels "1","2": 1 2
$ :'data.frame': 3 obs. of 3 variables:
..$ a: num [1:3] 0.970 0.989 0.176
..$ b: num [1:3] 0.1058 0.4570 -0.0772
..$ c: Factor w/ 3 levels "1","2","3": 1 2 3
$ :'data.frame': 4 obs. of 3 variables:
..$ a: num [1:4] 0.3692 0.7254 0.4861 0.0638
..$ b: num [1:4] 0.788 2.075 1.027 1.208
..$ c: Factor w/ 4 levels "1","2","3","4": 1 2 3 4
$ :'data.frame': 5 obs. of 3 variables:
..$ a: num [1:5] 0.109 0.333 0.837 0.277 0.587
..$ b: num [1:5] 0.9811 0.5324 -0.0905 0.1565 -0.7373
..$ c: Factor w/ 5 levels "1","2","3","4",..: 1 2 3 4 5
$ :'data.frame': 6 obs. of 3 variables:
..$ a: num [1:6] 0.420 0.334 0.865 0.177 0.493 ...
..$ b: num [1:6] 0.162 2.025 -0.704 0.961 1.790 ...
..$ c: Factor w/ 6 levels "1","2","3","4",..: 1 2 3 4 5 6
$ :'data.frame': 7 obs. of 3 variables:
..$ a: num [1:7] 0.507 0.155 0.348 0.660 0.312 ...
..$ b: num [1:7] 0.409 1.689 1.587 -0.331 -2.285 ...
..$ c: Factor w/ 7 levels "1","2","3","4",..: 1 2 3 4 5 6 7
$ :'data.frame': 8 obs. of 3 variables:
..$ a: num [1:8] 0.706 0.476 0.495 0.308 0.695 ...
..$ b: num [1:8] 0.421 -0.400 -1.370 0.988 1.520 ...
..$ c: Factor w/ 8 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8
$ :'data.frame': 9 obs. of 3 variables:
..$ a: num [1:9] 0.4822 0.9205 0.0415 0.2940 0.5009 ...
..$ b: num [1:9] 1.576 -1.476 -0.145 -0.953 0.407 ...
..$ c: Factor w/ 9 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9
$ :'data.frame': 10 obs. of 3 variables:
..$ a: num [1:10] 0.938 0.716 0.163 0.476 0.690 ...
..$ b: num [1:10] -0.706 -0.161 0.501 -1.014 1.615 ...
..$ c: Factor w/ 10 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10
> x.r <- do.call('rbind', x)
> str(x.r)
'data.frame': 55 obs. of 3 variables:
$ a: num 0.9863 0.8121 0.0772 0.9702 0.9895 ...
$ b: num -0.543 -0.349 -1.008 0.106 0.457 ...
$ c: Factor w/ 10 levels "1","2","3","4",..: 1 1 2 1 2 3 1 2 3 4 ...
>
>
On 4/29/07, 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.
>
--
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