Learn R Programming

Stack (version 2.0-1)

Stack: Stack Combine data frames with column names that do not completely intersect

Description

Stack Combine data frames with column names that do not completely intersect

Usage

Stack(df1, df2, return.data.frame = TRUE, dates = c("wave", "date"), origin = "1970-01-01", mixed.chr.factor = c("factor", "character"), verbose = FALSE)

Arguments

df1
a data.frame or a list
df2
a data.frame or a list
return.data.frame
defaults to TRUE and uses quickdf; regular data.frame checks are not done.
dates
character names of columns that are or should be class Date. These columns will be coerced to Date, handling the odd case where they are quoted chr days-since-origin.
origin
Origin for dates that are chr like "15218", which ought to be 2011-09-01. This is not uncommon for dates coming from SPSS.
mixed.chr.factor
when an element is mixed factor/character in df1 and df2, what should the result type be? Default="factor".
verbose
print extra information along the way about each column.

Value

a list of data vectors suitable for coercion to a data.frame. Because as.data.frame is extremely costly and memory-intensive, and Stacking often involves many such column-wise combinations, avoid this by returning a list.

Examples

Run this code
testdf1 <- data.frame(both.int=1:4,
                      expand.factor=c("blue", "yellow"),
                      mixed.fac.int=factor(letters[1:4]),
                      date=as.Date("1983-11-22"),
                      df1only=rep(1:2, each=2),
                      mixed.fac.chr=I(c("a","b","NA",NA)))
testdf2 <- data.frame(both.int=5:24,
                      expand.factor=factor(rep(c(1:4, NA), 4)),
                      mixed.fac.int=1:4,
                      date=as.Date("1981-09-24"),
                      df2only=factor(c("c", "d")),
                      mixed.fac.chr=c("a","b",NA,"c"))

levels(testdf2$mixed.fac.chr) <- letters #overleveled
## put levels in a different order than sort(levels) would do, but
## don't make it an ordered factor. Result needs to be ordered
## to preserve this.
levels(testdf2$expand.factor) <- c("green", "blue", "red", "yellow")
Stack(testdf1, testdf2)
Stack(testdf2, testdf1)

Run the code above in your browser using DataLab