R的所有列从因子更改为数字
问题说明
我正在处理一个大数据集,该数据集引起了一些麻烦,因为数据集中的某些列被视为因素.如何将所有列从因子转换为数字,而不必逐列执行?
I am working with a big dataset that is causing some trouble because some of the columns I the dataset are being treated as factors. How can I convert all of the columns from factor to numeric, without having to do that column by column??
我尝试应用一个小循环,但它返回 NA 值.以下是适用于该案例的示例数据:
I have tried to apply a small loop, but it returns NA values. Here's a sample data that applies to the case:
data <- structure(list(v1 = c(22.394, 43.72, 58.544, 56.877, 1.659, 29.142,
67.836, 68.851), v2 = c(144.373, 72.3, 119.418, 112.429, 35.779,
41.661, 166.941, 126.548), v3 = structure(c(33L, 29L, 33L, 5L,
13L, 31L, 5L, 8L), .Label = c("", "#VALUE!", "0", "1", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20",
"21", "22", "23", "24", "25", "26", "28", "29", "3", "30", "32",
"33", "4", "48", "5", "6", "7", "8", "9"), class = "factor"),
v4 = structure(c(24L, 6L, 22L, 23L, 16L, 22L, 23L, 26L), .Label = c("",
"-1", "-2", "-4", "#VALUE!", "0", "1", "10", "11", "12",
"13", "14", "15", "16", "17", "18", "19", "2", "24", "28",
"29", "3", "4", "5", "6", "7", "8", "9"), class = "factor")), .Names = c("v1",
"v2", "v3", "v4"), row.names = c("4", "5", "6", "7", "8", "9",
"10", "11"), class = "data.frame")
for (i in 1:ncol(data)){
data[,i] <- as.numeric(as.character(data[i]))
} ## returns NAs
我可以应用一些命令将所有这些列转换为数字类吗?
Is there some command that I can apply to turn all these columns into a numeric class?
正确答案
这行得通,但我认为您的数据有一个奇怪的字符或空格,这使它成为读取的因素.您可以尝试使用参数 stringsAsFactors = FALSE
进行读取.但仍然无法解决字符与数字读入的问题.这是一个修复:
This works but I'm thinking your data has an odd character or space, something that makes it read in as factor. You can try reading in with the argument stringsAsFactors = FALSE
. But still wouldn't address character vs numeric read in. Here's a fix:
data[] <- lapply(data, function(x) as.numeric(as.character(x)))
## > str(data)
## 'data.frame': 8 obs. of 4 variables:
## $ v1: num 22.39 43.72 58.54 56.88 1.66 ...
## $ v2: num 144.4 72.3 119.4 112.4 35.8 ...
## $ v3: num 7 4 7 10 18 5 10 13
## $ v4: num 5 0 3 4 18 3 4 7
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcakhhf
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
使用c++17更新时出现G++编译器警告
it1352 06-18 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
复制文件夹/文件而不修改属性?
it1352 07-15 -
Android微调工具-删除当前选择
it1352 06-20 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20