[R] rbind, data.frame, classes

Henrique Dallazuanna wwwhsd at gmail.com
Mon Mar 15 19:59:30 CET 2010


Try this:

do.call(rbind, lapply(list(list1, list2), as.data.frame))

On Mon, Mar 15, 2010 at 3:42 PM, Rob Forler <rforler at uchicago.edu> wrote:
> Hi,
>
> This has bugged me for a bit. First question is how to keep classes with
> rbind, and second question is how to properly return vecotrs instead of
> lists after turning an rbind of lists into a data.frame
>
> list1=list(a=2, b=as.Date("20090102", format="%Y%m%d"))
> list2=list(a=2, b=as.Date("20090102", format="%Y%m%d"))
>
> rbind(list1, list2) #this loses the posixct class. My first question is is
> there a way to do this without losing the classes?
>
> data.frame(rbind(list1, list2))
> data.frame(rbind(list1, list2))$a #this returns a list output  and not a
> vector output. (this is a huge pain)
> data.frame(rbind(list1, list2))[,c("a")] #also returns list output
>
> #> data.frame(rbind(list1, list2))[,c("a")]
> #$list1
> #[1] 1
> #
> #$list2
> #[1] 2
>
> # I would instead like [1] 1 2 like in the below example
>
>> vec1 = c(1,2)
>> vec2 = c(2,3)
>> data.frame(rbind(vec1, vec2))[,1] #this outputs as a vector which is what
> I want from the above list rbind.
>
> is it possible to easily do the above? I read over rbind but it doesn't seem
> to have any of the above fixes. Is there a different function that does this
> the way I want it to?
>
> Thanks,
> Rob
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list