将类从因子更改为数据框中多列的数字 [英] Change the class from factor to numeric of many columns in a data frame

查看:55
本文介绍了将类从因子更改为数据框中多列的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将大量列从因子更改为数字的最快/最佳方法是什么?

What is the quickest/best way to change a large number of columns to numeric from factor?

我使用了以下代码,但它似乎对我的数据进行了重新排序.

I used the following code but it appears to have re-ordered my data.

> head(stats[,1:2])
  rk                 team
1  1 Washington Capitals*
2  2     San Jose Sharks*
3  3  Chicago Blackhawks*
4  4     Phoenix Coyotes*
5  5   New Jersey Devils*
6  6   Vancouver Canucks*

for(i in c(1,3:ncol(stats))) {
    stats[,i] <- as.numeric(stats[,i])
}

> head(stats[,1:2])
  rk                 team
1  2 Washington Capitals*
2 13     San Jose Sharks*
3 24  Chicago Blackhawks*
4 26     Phoenix Coyotes*
5 27   New Jersey Devils*
6 28   Vancouver Canucks*

最好的方法是什么,而不是像这样命名每一列:

What is the best way, short of naming every column as in:

df$colname <- as.numeric(ds$colname)

推荐答案

根据 Ramnath 的回答,您遇到的行为是由于 as.numeric(x) 返回内部数字表示在 R 级别的因子 x.如果您想保留作为因子级别的数字(而不是它们的内部表示),您需要首先按照 Ramnath 的示例通过 as.character() 转换为字符.

Further to Ramnath's answer, the behaviour you are experiencing is that due to as.numeric(x) returning the internal, numeric representation of the factor x at the R level. If you want to preserve the numbers that are the levels of the factor (rather than their internal representation), you need to convert to character via as.character() first as per Ramnath's example.

您的 for 循环与 apply 调用一样合理,并且对于代码的意图可能更具可读性.只需更改这一行:

Your for loop is just as reasonable as an apply call and might be slightly more readable as to what the intention of the code is. Just change this line:

stats[,i] <- as.numeric(stats[,i])

阅读

stats[,i] <- as.numeric(as.character(stats[,i]))

这是FAQ 7.10 在 R 常见问题解答中.

This is FAQ 7.10 in the R FAQ.

HTH

这篇关于将类从因子更改为数据框中多列的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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