I have a matrix matrix
with two level groupings as illustrated in the row and column names.
UKC1_SS1 UKC1_SS2 UKC2_SS1 UKC2_SS2
UKC1_SS1 1 2 3 4
UKC1_SS2 5 6 7 8
UKC2_SS1 9 10 11 12
UKC2_SS2 13 14 15 16
I want to create with a table with the column and row sums based on the first four digits of the column and row names:
UKC1 UKC2
UKC1 14 22
UKC2 46 54
I tried calculating rowsums
and colSums
sequentially,
sum.matrix <- rowsum(matrix, substr(rownames(matrix), start = 1, stop = 4))
sum.matrix <- colSums(sum.matrix, substr(colnames(test), start = 1, stop = 4)
but I receive the following error message: Error in colSums(test, substr(colnames(test), start = 1, stop = 4)) : invalid 'na.rm' argument
When I run sum(is.na)
I confirm that there are NA values in matrix
.