首页 > 解决方案 > 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”似乎工作正常?

谢谢

标签: rlistdataframedata-manipulationplyr

解决方案


推荐阅读