R: convert asymmetric list to matrix - number of elements in each sub-list differ

后端 未结 1 623
青春惊慌失措
青春惊慌失措 2020-12-17 04:07

I have an asymmetric list, i.e., the number of elements in each sub-list differ. How can I convert the list to a matrix?

Below I begin with a symmetric list and con

相关标签:
1条回答
  • 2020-12-17 04:12

    First, extend each vector in your list with NAs to get vectors of the same length. Then create your matrix. For example:

    max.len <- max(sapply(my.list2, length))
    corrected.list <- lapply(my.list2, function(x) {c(x, rep(NA, max.len - length(x)))})
    mat <- do.call(rbind, corrected.list)
    
    0 讨论(0)
提交回复
热议问题