Suppose I have a vector or column of arbitrary length representing some grouping/factor variable with an arbitrary number of groups and arbitrary values for same along the lines of this:
假设我有一个任意长度的向量或列表示某个分组/因子变量具有任意数量的组和任意值沿着这条线:
a <- c(2,2,2,2,2,7,7,7,7,10,10,10,10,10)
a
[1] 2 2 2 2 2 7 7 7 7 10 10 10 10 10
How would I most easily turn that into this:
我怎么能轻易地把它变成这样:
a
[1] 1 1 1 1 1 2 2 2 2 3 3 3 3 3
1 个解决方案
#1
4
a <- c(2,2,2,2,2,7,7,7,7,10,10,10,10,10)
c(factor(a))
#[1] 1 1 1 1 1 2 2 2 2 3 3 3 3 3
Explanation:
解释:
A factor is just an integer vector with levels
attribute and a class attribute. c
removes attributes as a side effect. You could use as.numeric
or as.integer
instead of c
with similar or the same results, respectively.
因子只是一个具有level属性和class属性的整数向量。c删除属性作为副作用。你可以使用。数字或as.integer而不是具有相似或相同结果的c。
#1
4
a <- c(2,2,2,2,2,7,7,7,7,10,10,10,10,10)
c(factor(a))
#[1] 1 1 1 1 1 2 2 2 2 3 3 3 3 3
Explanation:
解释:
A factor is just an integer vector with levels
attribute and a class attribute. c
removes attributes as a side effect. You could use as.numeric
or as.integer
instead of c
with similar or the same results, respectively.
因子只是一个具有level属性和class属性的整数向量。c删除属性作为副作用。你可以使用。数字或as.integer而不是具有相似或相同结果的c。