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.
我有一个具有 195 个级别的 pid 因子变量,我想将其更改为每个唯一患者 ID 的数字变量。有人可以帮忙吗?下面的 TIA 是我的数据的样子;
当您使用as.numeric()R 将因子转换为数字时,不会使用您看到的标签,而是使用因子水平。例如,如果您有因子as.factor(c(1,2,5,7)),则级别仍然是 1、2、3、4,如第一类、第二类、第三类和第四类。使用实际标签的最简单方法是先将因子转换为字符串,然后将字符串转换为数值变量。
as.numeric()
as.factor(c(1,2,5,7))
NewVariable <- as.numeric(as.character(FactorVariable))