1) Below lapply adds zeros n to each component m , and sapply takes the first elements n each component m , changing the shape of the result into a matrix. Finally, we reverse the row order of the resulting matrix. This works even if m does not define a triangular matrix:
n <- length(m) sapply(lapply(m, c, numeric(n)), head, n)[n:1, ]
giving:
[,1] [,2] [,3] [,4] [1,] 0 0 0 10 [2,] 0 0 6 9 [3,] 0 3 5 8 [4,] 1 2 4 7
If n can be null, use rev(seq_len(n)) instead of n:1 .
2) Direct sapply also works. It adds each inverse component m with the corresponding number of zeros and converts it into a matrix:
sapply(m, function(v) c(numeric(n - length(v)), rev(v)))
source share