0

I have two factor variables in a data frame and the end goal is to combine both columns to find a list of the unique factors of both the columns combined (some of the factors in variable one are repeated in variable two). To do this I need a vector of length 2n to perform the 'unique' function on (stacked vector in code below). However I am having problems combining these two vectors using the 'stack' function.

x<-rep(c("a","b", "c"), each=3)
x<-as.vector(x)
y<-rep(c("a","b", "z"), each=3)
y<-as.vector(y)
combined<-data.frame(x,y)
stacked<-stack(combined) 
unique(stacked)

I don't understand the error message I am getting on the 2nd last line. I know this must be something so simple but I just can't see it! If anyone knows or has a more elegant way of solving this problem please reply!

4

1 回答 1

3

The answer lies in the help file at ?stack

 Note that ‘stack’ applies to _vectors_ (as determined by
 ‘is.vector’): non-vector columns (e.g., factors) will be ignored
 (with a warning as from R 2.15.0).  

> is.vector(factor("a"))
[1] FALSE

Try:

stacked <- stack(lapply(combined,as.character))
stacked

   values ind
1       a   x
2       a   x
3       a   x
4       b   x
5       b   x
6       b   x
...

..or as @Dwin points out, you could have created combined to be characters instead of factors in the first place by specifying stringsAsFactors=FALSE in your data.frame call.

于 2013-04-24T03:56:38.850 回答