我有一个矩阵,其中存在重复的字符列名。
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
我需要将所有列名相同的列合并成一列,即:
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
我已经尝试使用聚合函数中的简单求和,但是没有成功。有什么建议吗?谢谢。