可能重复:
How to convert a factor to an integer\numeric without a loss of information
我想计算(以及稍后绘制数量)数组的唯一值:
data = c(1,2,3,4,5,2.1,1,2,1,2,1,4,5,7,8,9,6,5,4,3,2,2,1)
uniCount = as.data.frame(table(data))
uniCount$cumsum = cumsum(uniCount$Freq)
str(uniCount)
plot (uniCount$data, uniCount$Freq)
plot (uniCount$data, uniCount$cumsum)
但是,列data
的值不是'数字'而是'因子'。对我来说,似乎数据类型因子是字符串的关联数组。当我使用as.numeric(uniCount$data)
时,结果给出“1 2 3 4 5 6 7 8 9 10”
如何将数据类型“Factor”转换为数据类型“numeric”? 或者我如何防止R将我的数值转换为Facotr?
答案 0 :(得分:3)
这可能适合你
as.numeric(attr(uniCount$data,"levels"))
或
as.numeric(levels(uniCount$data))
如果您不想要因素,请尝试
data = c(1,2,3,4,5,2.1,1,2,1,2,1,4,5,7,8,9,6,5,4,3,2,2,1)
uniCount = as.data.frame(table(data),stringsAsFactors =F)
uniCount$cumsum = cumsum(uniCount$Freq)
编辑:
感谢@Carl Witthoft,但?factor
表示建议使用as.numeric(levels(uniCount$data))[uniCount$data]
,效率稍高于as.numeric(as.character(uniCount$data))
"
答案 1 :(得分:0)
plyr
包可以在不获取字符/因子变量的情况下更轻松地执行此类操作。
library(plyr)
d <- data.frame(x=c(1,2,3,4,5,2.1,1,2,1,2,1,4,5,7,8,9,6,5,4,3,2,2,1))
ddply(d, "x", summarize, Freq=length(x))