r - R警告:“数据长度不是行数的子倍数或倍数”
问题描述
我正在使用 R 编程语言。
我有一个具有以下格式的“列表”(称为“l”):
$`1`
random_1 random_2 random_3 random_4 split_1 split_2 split_3 b c total
1: 80 85 85 90 0.4 0.4 0.4 0.3333333 0.4002006 0.4
$`2`
random_1 random_2 random_3 random_4 split_1 split_2 split_3 b c total
1: 85 85 85 90 0.4 0.4 0.4 0.3333333 0.4002006 0.4
$`3`
random_1 random_2 random_3 random_4 split_1 split_2 split_3 a b c total
1: 90 85 85 90 0.4 0.4 0.4 0 0.3333333 0.3985944 0.398
$`4`
random_1 random_2 random_3 random_4 split_1 split_2 split_3 a b c total
1: 95 85 85 90 0.4 0.4 0.4 0 0.3333333 0.3985944 0.398
当您使用“str”语句时,可以查看有关列表的更多信息:
str(l)
List of 20
$ 1 :Classes ‘data.table’ and 'data.frame': 1 obs. of 10 variables:
..$ random_1: num 80
..$ random_2: num 85
..$ random_3: num 85
..$ random_4: num 90
..$ split_1 : num 0.4
..$ split_2 : num 0.4
..$ split_3 : num 0.4
..$ b : num 0.333
..$ c : num 0.4
..$ total : num 0.4
..- attr(*, ".internal.selfref")=<externalptr>
..- attr(*, "sorted")= chr [1:7] "random_1" "random_2" "random_3" "random_4" ...
$ 2 :Classes ‘data.table’ and 'data.frame': 1 obs. of 10 variables:
..$ random_1: num 85
..$ random_2: num 85
..$ random_3: num 85
..$ random_4: num 90
..$ split_1 : num 0.4
..$ split_2 : num 0.4
..$ split_3 : num 0.4
..$ b : num 0.333
..$ c : num 0.4
..$ total : num 0.4
..- attr(*, ".internal.selfref")=<externalptr>
..- attr(*, "sorted")= chr [1:7] "random_1" "random_2" "random_3" "random_4" ...
$ 3 :Classes ‘data.table’ and 'data.frame': 1 obs. of 11 variables:
..$ random_1: num 90
..$ random_2: num 85
..$ random_3: num 85
..$ random_4: num 90
..$ split_1 : num 0.4
..$ split_2 : num 0.4
..$ split_3 : num 0.4
..$ a : num 0
..$ b : num 0.333
..$ c : num 0.399
..$ total : num 0.398
..- attr(*, ".internal.selfref")=<externalptr>
..- attr(*, "sorted")= chr [1:7] "random_1" "random_2" "random_3" "random_4" ...
使用此 stackoverflow 帖子:将列表转换为数据框,我尝试了三种不同的方法将此“列表”转换为“数据框”:
方法一:没用
df = do.call(rbind.data.frame, l)
Error in (function (..., deparse.level = 1, make.row.names = TRUE, stringsAsFactors = default.stringsAsFactors(), :
numbers of columns of arguments do not match
方法 2:部分有效,但有警告
df <- data.frame(matrix(unlist(l), nrow=length(l), byrow=TRUE))
Warning message:
In matrix(unlist(l), nrow = length(l), byrow = TRUE) :
data length [212] is not a sub-multiple or multiple of the number of rows [20]
df <- data.frame(matrix(unlist(l), nrow=length(l), byrow=TRUE))
出于某种原因,“方法 2”中的代码在结果数据框中放置了几个 0。
方法3:完全有效(据我所知)
library(plyr)
df <- ldply (l, data.frame)
head(df)
.id random_1 random_2 random_3 random_4 split_1 split_2 split_3 b c total a
1 1 80 85 85 90 0.4 0.4 0.4 0.3333333 0.4002006 0.400 NA
2 2 85 85 85 90 0.4 0.4 0.4 0.3333333 0.4002006 0.400 NA
3 3 90 85 85 90 0.4 0.4 0.4 0.3333333 0.3985944 0.398 0
4 4 95 85 85 90 0.4 0.4 0.4 0.3333333 0.3985944 0.398 0
5 5 100 85 85 90 0.4 0.4 0.4 0.3333333 0.3985944 0.398 0
6 6 80 90 85 90 0.4 0.4 0.4 0.3333333 0.4004024 0.400 NA
问题:有谁知道为什么“方法1”和“方法2”不能正常工作,但“方法3”似乎工作正常?
谢谢
解决方案
推荐阅读
- javascript - 如何根据用户时区设置提醒并根据时区发送通知?
- php - 当 preg_quote 中的模式和主题时 Preg_match 不起作用 - PHP
- php - 当我重定向到外部 url 时,请求在正常模式下的浏览器中被取消,但在禁用安全模式下工作
- php - 如何删除 symfony 表单类型的输入字段中的空格?
- python - 通过在网页中导航不同的选项卡并将数据提取到 Dataframe 来进行 Web 抓取
- powershell - 在 PowerShell 脚本中从 Sqlcmd 获取错误
- c# - 是否可以通过其他一些 Windows 服务或相同的 Windows 服务启动/停止 Windows 服务?
- javascript - 我正在为我的学校项目创建一个样机电影网站。我遇到了一些与“选择”表单和 JavaScript 代码相关的问题
- ruby-on-rails - 如果未上传载波图像,则添加默认图像
- c++ - 应用程序在 Windows 10 上的 boost::filesystem::exist 和 CoInitializeSecurity 调用上自行启动