Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有一个任意长度的向量或列,它代表一些分组/因子变量,具有任意数量的组和任意值,如下所示:
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
我如何最容易地将其变成这样:
a [1] 1 1 1 1 1 2 2 2 2 3 3 3 3 3
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
解释:
因子只是具有levels属性和类属性的整数向量。c删除属性作为副作用。您可以分别使用as.numeric或as.integer代替c相似或相同的结果。
levels
c
as.numeric
as.integer