我正在尝试将数据帧(p2)中的因子(tickets_other)转换为整数。遵循 R 帮助指南以及其他人的其他建议,此代码应该可以工作:
as.numeric(levels(p2$tickets_other))[p2$tickets_other]
该列确实包含 NA,因此我收到警告:
Warning message:
NAs introduced by coercion
这很好,但在将其强制为数字之后,它仍然被视为一个因素:
class(p2$tickets_other)
[1] "factor"
如果我使用 as.numeric(as.character.()),也会出现同样的结果:
as.numeric(as.character(p2$tickets_other))
Warning message:
NAs introduced by coercion
class(p2$tickets_other)
[1] "factor"