2

I am working with the R programming language.

I have a "list" (called "l") that has the following format:

$`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

When you use the "str" statement, more information about the list can be viewed:

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" ...

Using this stackoverflow post : Convert a list to a data frame , I tried three different ways to convert this "list" into a "data frame":

Method 1: Did Not Work

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

Method 2: Partially Worked, but with Warnings

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))

For some reason, the code from "Method 2" placed several 0's in the resulting data frame.

Method 3: Fully Works (to my knowledge)

    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

Question: Does anyone know why "Method 1" and "Method 2" did not work properly, but "Method 3" seems to work properly?

Thanks

stats_noob
  • 5,401
  • 4
  • 27
  • 83
  • 2
    Have you tried with `rbindlist(l, fill = TRUE)` – akrun Jul 28 '21 at 20:56
  • 2
    list $3 in your data.frame is missing the "a" column, but $1 and $2 have it. So your data.frames are of different lengths and can not be easily binded. @akruns comment should work – GuedesBF Jul 28 '21 at 21:04
  • Akrun's comment worked perfectly! I will have to remember this for the future! – stats_noob Jul 29 '21 at 03:20

0 Answers0