[R] stacking data frames with different variables

hadley wickham h.wickham at gmail.com
Sun Sep 9 16:11:28 CEST 2007


Have a look at rbind.fill in the reshape package.

Hadley

On 9/9/07, Muenchen, Robert A (Bob) <muenchen at utk.edu> wrote:
> Hi All,
>
> If I need to stack two data frames, I can use rbind, but it requires
> that all variables exist in both sets. I can make that happen, but other
> stat packages would figure out where the differences were, add the
> missing variables to each, set their values to missing and stack them.
> Is there a more automatic way to do that in R?
>
> Below is an example program.
>
> Thanks,
> Bob
>
> # Top data frame has two variables.
> x <- c(1,2)
> y <- c(1,2)
>
> top <- data.frame(x,y)
> top
>
> # Bottom data frame has only one of them.
> x <- c(3,4)
> bottom <- data.frame(x)
> bottom
>
> # So rbind won't work.
> rbind(top, bottom)
>
> # After figuring out where the mismatches are I can
> # make the two DFs the same manually.
> bottom <- data.frame( bottom, y=NA)
> bottom
>
> # Now I get the desired result.
> both <- rbind(top,bottom)
> both
>
> =========================================================
> Bob Muenchen (pronounced Min'-chen), Manager
> Statistical Consulting Center
> U of TN Office of Information Technology
> 200 Stokely Management Center, Knoxville, TN 37996-0520
> Voice: (865) 974-5230
> FAX: (865) 974-4810
> Email: muenchen at utk.edu
> Web: http://oit.utk.edu/scc,
> News: http://listserv.utk.edu/archives/statnews.html
>
> ______________________________________________
> 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.
>


-- 
http://had.co.nz/



More information about the R-help mailing list