Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine columns in matrix having same column name

Tags:

r

aggregate

I have a matrix with columns that duplicate character column names.

set.seed(1)
m <- matrix(sample(1:10,12,replace=TRUE), nrow = 3, ncol = 4, byrow = TRUE,
       dimnames = list(c("s1", "s2", "s3"),c("x", "y","x","y")))

m
   x y  x  y
s1 3 4  6 10
s2 3 9 10  7
s3 7 1  3  2

I need to sum all columns with the same column name into only one column i.e.

m <- matrix(c(9,14,13,16,10,3), nrow = 3, ncol = , byrow = TRUE,dimnames = list(c("s1", "s2", "s3"),c("x", "y")))

    x  y
s1  9 14
s2 13 16
s3 10  3

I have had a play with the simple sum in the aggregate function but haven't had any luck. Any advice? Thanks.

like image 854
Elizabeth Avatar asked Dec 08 '22 22:12

Elizabeth


2 Answers

Well, this solution won't win any awards for transparency of code, but I rather like it:

nms <- colnames(m)
m %*% sapply(unique(nms),"==", nms)
#     x  y
# s1  9 14
# s2 13 16
# s3 10  3

It works by constructing a matrix that forms appropriate linear combinations of m's columns. To see how it works, pick apart the second line into its two component matrices, which are multiplied together using %*%, like this:

 -          -      -   -
|  3 4  6 10 |    | 1 0 |
|  3 9 10  7 |    | 0 1 |
|  7 1  3  2 |    | 1 0 |
 -          -     | 0 1 |
                   -   -
like image 65
Josh O'Brien Avatar answered Jan 01 '23 04:01

Josh O'Brien


nms <- colnames(m)
sapply(unique(nms), function(i)rowSums(m[, nms==i]))

   x  y
s1  9 14
s2 13 16
s3 10  3
like image 38
Andrie Avatar answered Jan 01 '23 02:01

Andrie