Stack: Stack Combine data frames with column names that do not...

Description Usage Arguments Value Examples

View source: R/stack.R

Description

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

Usage

1
2
3
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
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)

Example output

   both.int expand.factor mixed.fac.int       date mixed.fac.chr df1only
1         1          blue             a 1983-11-22             a       1
2         2        yellow             b 1983-11-22             b       1
3         3          blue             c 1983-11-22          <NA>       2
4         4        yellow             d 1983-11-22          <NA>       2
5         5         green             a 1981-09-24             a      NA
6         6          blue             b 1981-09-24             b      NA
7         7           red             c 1981-09-24          <NA>      NA
8         8        yellow             d 1981-09-24             c      NA
9         9          <NA>             a 1981-09-24             a      NA
10       10         green             b 1981-09-24             b      NA
11       11          blue             c 1981-09-24          <NA>      NA
12       12           red             d 1981-09-24             c      NA
13       13        yellow             a 1981-09-24             a      NA
14       14          <NA>             b 1981-09-24             b      NA
15       15         green             c 1981-09-24          <NA>      NA
16       16          blue             d 1981-09-24             c      NA
17       17           red             a 1981-09-24             a      NA
18       18        yellow             b 1981-09-24             b      NA
19       19          <NA>             c 1981-09-24          <NA>      NA
20       20         green             d 1981-09-24             c      NA
21       21          blue             a 1981-09-24             a      NA
22       22           red             b 1981-09-24             b      NA
23       23        yellow             c 1981-09-24          <NA>      NA
24       24          <NA>             d 1981-09-24             c      NA
   df2only
1     <NA>
2     <NA>
3     <NA>
4     <NA>
5        c
6        d
7        c
8        d
9        c
10       d
11       c
12       d
13       c
14       d
15       c
16       d
17       c
18       d
19       c
20       d
21       c
22       d
23       c
24       d
Warning messages:
1: 'expand.factor': coerced to ordered factor 
2: 'expand.factor': factor levels expanded: check levels/codes! 
3: 'mixed.fac.int': mixed factor-integer: assigned levels from 'df1' 
4: 'mixed.fac.chr': new levels added from character values in 'df1' 
   both.int expand.factor mixed.fac.int       date mixed.fac.chr df2only
1         5         green             a 1981-09-24             a       c
2         6          blue             b 1981-09-24             b       d
3         7           red             c 1981-09-24          <NA>       c
4         8        yellow             d 1981-09-24             c       d
5         9          <NA>             a 1981-09-24             a       c
6        10         green             b 1981-09-24             b       d
7        11          blue             c 1981-09-24          <NA>       c
8        12           red             d 1981-09-24             c       d
9        13        yellow             a 1981-09-24             a       c
10       14          <NA>             b 1981-09-24             b       d
11       15         green             c 1981-09-24          <NA>       c
12       16          blue             d 1981-09-24             c       d
13       17           red             a 1981-09-24             a       c
14       18        yellow             b 1981-09-24             b       d
15       19          <NA>             c 1981-09-24          <NA>       c
16       20         green             d 1981-09-24             c       d
17       21          blue             a 1981-09-24             a       c
18       22           red             b 1981-09-24             b       d
19       23        yellow             c 1981-09-24          <NA>       c
20       24          <NA>             d 1981-09-24             c       d
21        1          blue             a 1983-11-22             a    <NA>
22        2        yellow             b 1983-11-22             b    <NA>
23        3          blue             c 1983-11-22          <NA>    <NA>
24        4        yellow             d 1983-11-22          <NA>    <NA>
   df1only
1       NA
2       NA
3       NA
4       NA
5       NA
6       NA
7       NA
8       NA
9       NA
10      NA
11      NA
12      NA
13      NA
14      NA
15      NA
16      NA
17      NA
18      NA
19      NA
20      NA
21       1
22       1
23       2
24       2
Warning messages:
1: 'expand.factor': coerced to ordered factor 
2: 'expand.factor': factor levels expanded: check levels/codes! 
3: 'mixed.fac.int': mixed integer-factor: assigned levels from 'df2' 
4: 'mixed.fac.chr': new levels added from character values in 'df2' 

Stack documentation built on May 2, 2019, 2:38 a.m.

Related to Stack in Stack...