我试图将数据框(p2)中的一个因子(tickets_other)转换为整数。继将R帮助指导,以及来自其他其他方面的建议,该代码应工作:将因子强制转换为整数的问题
as.numeric(levels(p2$tickets_other))[p2$tickets_other]
列不包含港定居,所以我得到一个警告:
Warning message:
NAs introduced by coercion
这很好,但是它强迫为数字后,它仍然读取的一个因素:(as.character())
class(p2$tickets_other)
[1] "factor"
如果我使用as.numeric同样的结果发生了:
as.numeric(as.character(p2$tickets_other))
Warning message:
NAs introduced by coercion
class(p2$tickets_other)
[1] "factor"
请提供一些数据 – 2014-09-24 17:31:42
这里命令'head(p2)'或'head(p2 $ tickets_other)'的输出结果。 – rnso 2014-09-24 17:34:37
不看你的数据。我猜你需要'as.numeric(as.character(p2 $ tickets_other))' – Vlo 2014-09-24 17:53:30