Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble understanding how stack() works

Tags:

r

I can't wrap my head around the documentation for ?stack and why it's not working. Consider:

> set.seed(1)
> x1 = sample(c(letters[1:5], NA), size=10, replace=TRUE)
> x2 = sample(c(letters[1:5], NA), size=10, replace=TRUE)
> is.vector(x1)
[1] TRUE
> rbind(x1, x2)
   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
x1 "b"  "c"  "d"  NA   "b"  NA   NA   "d"  "d"  "a"  
x2 "b"  "b"  "e"  "c"  "e"  "c"  "e"  NA   "c"  "e"  
> stack(x1, x2)
Error in rep.int(names(x), lapply(x, length)) : invalid 'times' value
> stack(list(x1, x2))
Error in rep.int(names(x), lapply(x, length)) : invalid 'times' value
> df = data.frame(x1=x1, x2=x2)
> stack(df)
Error in stack.data.frame(df) : no vector columns were selected

Here is what I want:

values  ind
   "b" "x1"
   "c" "x1"
   "d" "x1"
    NA "x1"

    ... etc.
like image 553
gung - Reinstate Monica Avatar asked Sep 30 '13 20:09

gung - Reinstate Monica


2 Answers

x needs to be a named list:

stack(list(x1= x1,x2 = x2))
like image 80
joran Avatar answered Oct 12 '22 14:10

joran


Well, first off you are passing a matrix argument to stack when its help page is asking for: "a list or data frame to be stacked or unstacked." Furthermore if you make it into a dataframe with the default setting for stringsAsFactors it will fail with a very uninformative error message.

 d=data.frame( x1=x1,x2=x2) 
 stack( d , select=c(x1,x2) )
#Error in stack.data.frame(x, ...) : no vector columns were selected


 d=data.frame( x1=x1,x2=x2, stringsAsFactors=FALSE)
 stack( d , select=c(x1,x2) )
#----------
   values ind
1       b  x1
2       c  x1
3       d  x1
4    <NA>  x1
5       b  x1
6    <NA>  x1
7    <NA>  x1
8       d  x1
9       d  x1
10      a  x1
11      b  x2
12      b  x2
13      e  x2
14      c  x2
15      e  x2
16      c  x2
17      e  x2
18   <NA>  x2
19      c  x2
20      e  x2
like image 25
IRTFM Avatar answered Oct 12 '22 14:10

IRTFM