将因子转换为整数 [英] Convert factor to integer

查看:112
本文介绍了将因子转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用reshape包操作数据框。当使用融合函数时,它会对我的值列进行分解,这是一个问题,因为这些值的子集是我希望能够对其执行操作的整数。

I am manipulating a data frame using the reshape package. When using the melt function, it factorizes my value column, which is a problem because a subset of those values are integers that I want to be able to perform operations on.

有没有人知道将一个因子强制转换为整数的方法?使用 as.character()会将其转换为正确的字符,但是我无法立即对其执行操作,并且 as.integer() as.numeric()会将其转换为系统存储该因子的数字,这是无用的。

Does anyone know of a way to coerce a factor into an integer? Using as.character() will convert it to the correct character, but then I cannot immediately perform an operation on it, and as.integer() or as.numeric() will convert it to the number that system is storing that factor as, which is not helpful.

谢谢!

杰夫

推荐答案

你可以结合这两个功能;强迫字符从数字开始:

You can combine the two functions; coerce to characters thence to numerics:

> fac <- factor(c("1","2","1","2"))
> as.numeric(as.character(fac))
[1] 1 2 1 2

这篇关于将因子转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆