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.
我正在使用 reshape 包操作数据框。使用 melt 函数时,它会分解我的值列,这是一个问题,因为这些值的子集是我希望能够对其执行操作的整数。
有谁知道将一个因子强制转换为整数的方法?Usingas.character()会将其转换为正确的字符,但是我无法立即对其执行操作,as.integer()或者as.numeric()将其转换为系统存储该因子的数字,这没有帮助。
as.character()
as.integer()
as.numeric()
谢谢!
杰夫
直接从帮助页面引用factor:
factor
建议将因子 f 转换为其原始数值,as.numeric(levels(f))[f]并且效率略高于as.numeric(as.character(f)).
as.numeric(levels(f))[f]
as.numeric(as.character(f))
您可以将这两个功能结合起来;强制转换为字符,然后转换为数字:
> fac <- factor(c("1","2","1","2")) > as.numeric(as.character(fac)) [1] 1 2 1 2