可以使用as.integer()函数将因子变量转换为整数,然后再将整数加1即可将五个水平转换为1-5。例如,假设有一个名为my_factor的因子变量,可以使用以下代码将其转换为1-5:

my_factor <- factor(c('A', 'B', 'C', 'D', 'E'))
my_factor_int <- as.integer(my_factor) + 1
my_factor_int

输出结果为:

[1] 2 3 4 5 6

其中,因子变量的五个水平 'A'、'B'、'C'、'D'、'E' 分别被转换为了整数2、3、4、5、6。

R语言快速将因子变量水平转换为数字(1-5)

原文地址: https://www.cveoy.top/t/topic/lL0f 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录