I have the following list that I would like to transform into the matrix M:
L<-list(c(2L, 29L, 30L), c(1L, 3L, 30L, 31L), c(2L, 31L, 32L), c(5L,
60L), c(4L, 6L, 60L, 61L), c(5L, 7L, 61L, 62L))
[[1]]
[1] 2 29 30
[[2]]
[1] 1 3 30 31
[[3]]
[1] 2 31 32
[[4]]
[1] 5 60
[[5]]
[1] 4 6 60 61
[[6]]
[1] 5 7 61 62
Matrix M:
2 29 30 NA
1 3 30 31
2 31 32 NA
5 60 NA NA
4 6 60 61
5 7 61 62
I want each list to be a row of the matrix and to fill in any missing values with NA.
>Solution :
Append NA at the end of each list element based on the max length of the list element with length<- and then use rbind with do.call
mx <- max(lengths(L))
do.call(rbind, lapply(L, `length<-`, mx))
-output
[,1] [,2] [,3] [,4]
[1,] 2 29 30 NA
[2,] 1 3 30 31
[3,] 2 31 32 NA
[4,] 5 60 NA NA
[5,] 4 6 60 61
[6,] 5 7 61 62