[R] stacking data frames with different variables

Muenchen, Robert A (Bob) muenchen at utk.edu
Sun Sep 9 15:59:25 CEST 2007


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



More information about the R-help mailing list