1
votes

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 .

1

1 Answers

6
votes

We can do the sum with xtabs after changing the dimnames with the substr of 1st 4 characters

dimnames(m1) <- lapply(dimnames(m1), substr, 1, 4)
xtabs(Freq~ Var1 + Var2, as.data.frame.table(m1))
#      Var2
#Var1   UKC1 UKC2
#  UKC1   14   22
#  UKC2   46   54

data

m1 <- structure(c(1L, 5L, 9L, 13L, 2L, 6L, 10L, 14L, 3L, 7L, 11L, 15L, 
4L, 8L, 12L, 16L), .Dim = c(4L, 4L), .Dimnames = list(c("UKC1_SS1", 
"UKC1_SS2", "UKC2_SS1", "UKC2_SS2"), c("UKC1_SS1", "UKC1_SS2", 
 "UKC2_SS1", "UKC2_SS1.1")))