[英]stacking character vectors in R
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). 为此,我需要一个长度为2n的矢量来执行“唯一”功能(下面代码中的堆叠矢量)。 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! 如果有人知道或有更优雅的解决方法,请回复!
The answer lies in the help file at ?stack
答案在于?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. ..或@Dwin指出,可以通过在data.frame
调用中指定stringsAsFactors=FALSE
来首先创建combined
字符而不是因子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.