What is equivalent to do.call (rbind, list)?

I got in my real data warning number of columns of result is not a multiple of vector length (arg 1), although my list has a unique number of columns when I useddo.call("rbind"

I want to try something else that produces the same output as do.call (rbind, list) to check if there is a problem in my list or not.

Example

       n = c(2, 3, 5,4) 
       n1 = c(2, 7, 4,6) 
       n2 = c(NA, NA, NA,NA) 
       x = list(n, n1, n2)
      dat <- do.call("rbind", x)

I tried this:

      df=matrix(as.numeric(unlist(x)), nrow= 3)

but

      identical(dat,df)
     > identical(dat,df)
      [1] FALSE

PS: I do not want to change class or str of my list

+4
source share
2 answers

Just a note: looking at

> dat
     [,1] [,2] [,3] [,4]
[1,]    2    3    5    4
[2,]    2    7    4    6
[3,]   NA   NA   NA   NA
> df
     [,1] [,2] [,3] [,4]
[1,]    2    4    4   NA
[2,]    3    2    6   NA
[3,]    5    7   NA   NA

I am not surprised by the result that

identical(dat,df)
[1] FALSE

However look

df=matrix(as.numeric(unlist(x)), nrow= 3, byrow = T)
identical(dat,df)
[1] TRUE

Alternatives do.call(rbind, list)

If you are looking for an alternative do.call, check out dplyr::bind_rows(which is powered by data frames and is reasonably efficient). The second option may be Reduceas in:

Reduce(rbind, x)
     [,1] [,2] [,3] [,4]
init    2    3    5    4
        2    7    4    6
       NA   NA   NA   NA

- data.table::rbindlist, (!). , , , . .

library(data.table)
rbindlist(list(x))
   V1 V2 V3
1:  2  2 NA
2:  3  7 NA
3:  5  4 NA
4:  4  6 NA

t(), .

, , , -

sapply(x, length)
[1] 4 4 4

, , , ncol , length. , names .

+6

byrow = TRUE:

df=matrix(as.numeric(unlist(x)), nrow= 3, byrow = TRUE)
+4

All Articles