我知道还有其他问题可以解决这个问题,例如How to convert a factor to integer\numeric without loss of information?。但是这些问题提出的解决办法都不适合我。 所以我有这个data.frame,我应该使用as.numeric将其转换为数字,但是在运行循环时,却收到了此错误消息
In Ops.factor(x[i, 1], 15) : ‘<=’ not meaningful for factors
所以我再次尝试使用可以在互联网上找到的答案
as.numeric(as.character(tobacco))
和
as.numeric(paste(tobacco))
但是当我查看它们时,我得到的东西看起来像这样
这是我的data.frame:
structure(list(V1 = structure(c(12L, 13L, 4L, 17L, 4L, 4L, 12L,
4L, 12L, 8L, 16L, 4L, 12L, 8L, 4L, 8L, 4L, 7L, 6L, 4L, 12L, 4L,
4L, 1L, 16L, 4L, 4L, 4L, 17L, NA, 1L, 3L, 6L, 6L, 7L, 6L, 15L,
17L, 6L, 16L, 3L, 15L, NA, 12L, 4L, 7L, 6L, 6L, 11L, 8L, 6L,
7L, 6L, 6L, 6L, 4L, 12L, 6L, 6L, 12L, 16L, 8L, 4L, 8L, 3L, 10L,
8L, 12L, 6L, 8L, 14L, 5L, 16L, 12L, 16L, 16L, 8L, 4L, 12L, 4L,
14L, 15L, 4L, 1L, 12L, 17L, 16L, 12L, 17L, 8L, 17L, 12L, 6L,
NA, 16L, 6L, 16L, 4L, 6L, 17L, 16L, 10L, 4L, 16L, 12L, 6L, 8L,
8L, 8L, 16L, 16L, 4L, 4L, 16L, 12L, 12L, 8L, 8L, 8L, 4L, 15L,
17L, 4L, 12L, 4L, 6L, 12L, 4L, 12L, 16L, 12L, 7L, 17L, 4L, 17L,
NA, 16L, 4L, 14L, 15L, 16L, 16L, 4L, 16L, 4L, 12L, 16L, 12L,
4L, 4L, 6L, 14L, 4L, 12L, 8L, 16L, 17L, 16L, 17L, 4L, NA, 4L,
4L, 12L, 7L, 8L, 4L, 4L, 12L, 17L, 8L, 6L, 16L, 4L, 4L, 13L,
4L, 4L, 4L, 12L, 3L, 12L, 4L, NA, 16L, 16L, 4L, 4L, 20L, 4L,
11L, 16L, 15L, 4L, 12L, 17L, 6L, 19L, 14L, 6L, 8L, 12L, 21L,
16L, 4L, 16L, 18L, 2L, 14L, 17L, 6L, 12L, 6L, 17L, 16L, 6L, 16L,
12L, 8L, 7L, 8L, 6L, 12L, 4L, 17L, 16L, 1L, 4L, 4L, 4L, 12L,
8L, 12L, 16L, 12L, 4L, 17L, 18L, 16L, 3L, 17L, 12L, 13L, NA,
4L, 16L, 8L, 4L, 12L, 12L, 12L, 6L, 10L, 6L, 12L, 4L, 10L, 16L,
4L, 4L, 12L, 3L, 4L, 17L, 9L, 13L, 17L, 11L, NA, 8L, 12L, 7L,
12L, 15L), .Label = c("0", "0.5", "1", "10", "120", "15", "2",
"20", "23", "25", "3", "30", "35", "40", "45", "5", "60", "7",
"75", "8", "90", "max 15"), class = "factor")), .Names = "V1", row.names = c(NA,
-274L), class = "data.frame")
有什么想法吗?谢谢 !