I have many matrices that all have the same column names but different numbers of rows. I would like them all to be uniform length, and have the rows be ordered in consecutive increasing order by the first column. See two examples of matrices that need alteration below:
count 0 1 2 3 4 5 6 7
[1,] 0 0 2 0 1 0 0 0 0
[2,] 1 1 1 4 0 1 0 1 0
[3,] 2 1 1 2 0 2 0 1 2
[4,] 3 0 1 0 0 0 0 0 0
[5,] 4 0 0 0 4 0 0 0 0
[6,] 5 0 0 0 0 0 3 0 0
[7,] 8 0 0 0 0 0 1 0 0
count 0 1 2 3 4 5 6 7
[1,] 0 0 2 0 1 0 0 0 0
[2,] 1 1 1 4 0 1 0 1 0
[3,] 2 1 1 2 0 2 0 1 2
[4,] 3 0 1 0 0 0 0 0 0
[5,] 4 0 0 0 4 0 0 0 0
[6,] 7 0 0 0 0 0 3 0 0
The max value of the count column should always be 8; I would like to insert vectors into the right locations starting with the appropriate consecutive value followed by eight trailing zeroes. The above matrices should look like this:
count 0 1 2 3 4 5 6 7
[1,] 0 0 2 0 1 0 0 0 0
[2,] 1 1 1 4 0 1 0 1 0
[3,] 2 1 1 2 0 2 0 1 2
[4,] 3 0 1 0 0 0 0 0 0
[5,] 4 0 0 0 4 0 0 0 0
[6,] 5 0 0 0 0 0 3 0 0
[7,] 6 0 0 0 0 0 0 0 0 # this row has been inserted
[8,] 7 0 0 0 0 0 0 0 0 # this row has been inserted
[9,] 8 0 0 0 0 0 1 0 0
count 0 1 2 3 4 5 6 7
[1,] 0 0 2 0 1 0 0 0 0
[2,] 1 1 1 4 0 1 0 1 0
[3,] 2 1 1 2 0 2 0 1 2
[4,] 3 0 1 0 0 0 0 0 0
[5,] 4 0 0 0 4 0 0 0 0
[6,] 5 0 0 0 0 0 0 0 0 # this row has been inserted
[7,] 6 0 0 0 0 0 0 0 0 # this row has been inserted
[8,] 7 0 0 0 0 0 3 0 0
[9,] 8 0 0 0 0 0 0 0 0 # this row has been inserted
The matrices are embedded in a long list, many of which already have 9 rows and do not need to be modified, so ideally this solution could be vectorized to work across the list rather than in a for loop. Below is some gross code to produce a toy list of 4 matrices (with the first two elements corresponding to the problematic matrices shown above).
list(matrix(c(0,1,2,3,4,5,8,0,1,1,0,0,0,0,2,1,1,1,0,0,0,0,4,2,0,0,0,0,1,0,0,0,4,0,0,0,1,2,0,0,0,0,0,0,0,0,0,3,1,0,1,1,0,0,0,0,0,0,2,0,0,0,0),nrow=7,ncol=9,dimnames=(list(character(0),c("count",0:7)))),matrix(c(0,1,2,3,4,7,0,1,1,0,0,0,2,1,1,1,0,0,0,4,2,0,0,0,1,0,0,0,4,0,0,1,2,0,0,0,0,0,0,0,0,3,0,1,1,0,0,0,0,0,2,0,0,0),nrow=6,ncol=9,dimnames=(list(character(0),c("count",0:7)))),matrix(c(0,1,2,3,4,5,6,7,8,0,1,0,0,1,0,0,0,0,2,1,1,1,0,0,0,0,0,4,2,0,0,0,0,0,0,1,0,0,0,0,0,4,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,3,1,0,1,1,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0),nrow=9,ncol=9,dimnames=(list(character(0),c("count",0:7)))),matrix(c(0,1,2,3,4,5,6,7,8,0,1,0,0,1,0,0,0,0,2,1,1,1,0,0,0,0,0,4,2,0,0,0,0,0,0,1,0,0,0,0,0,4,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,3,1,0,1,1,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0),nrow=9,ncol=9,dimnames=(list(character(0),c("count",0:7)))))
This is similar to another question I asked recently - hopefully I can keep these matrices in a nice simple empty list and not have to pre-specify the dims as an array-based solution might require. Thanks in advance!