Re: [R] creating a data frame from a list

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Wed 05 Jul 2006 - 15:18:37 EST

Create a zoo object from each component of lst using the names as the "times". Then merge them together using merge.zoo, set the rownames from the times and extract out the data:

library(zoo)
z <- do.call(merge, lapply(lst, function(x) zoo(x, names(x)))) rownames(z) <- time(z)
coredata(z)

On 7/5/06, Dimitri Szerman <dimitrijoe@ipea.gov.br> wrote:
> Dear all,
>
> I have a list with three (named) numeric vectors:
>
> > lst = list(a=c(A=1,B=8) , b=c(A=2,B=3,C=0), c=c(B=2,D=0) )
> > lst
> $a
> A B
> 1 8
>
> $b
> A B C
> 2 3 0
>
> $c
> B D
> 2 0
>
>
> Now, I'd love to use this list to create the following data frame:
>
> > dtf = data.frame(a=c(A=1,B=8,C=NA,D=NA),
> + b=c(A=2,B=3,C=0,D=NA),
> + c=c(A=NA,B=2,C=NA,D=0) )
>
> > dtf
> a b c
> A 1 2 NA
> B 8 3 2
> C NA 0 NA
> D NA NA 0
>
> That is, I wish to "merge" the three vectors in the list into a data frame
> by their "(row)"names. Any suggestions? Thanks in advance for youR-Help.
>
> Dimitri
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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
>



R-help@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 Received on Wed Jul 05 15:22:46 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 05 Jul 2006 - 16:16:13 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.