我有以下类型的矩阵:
son.mat <- matrix(data = rep(rnorm(5), 9), nrow = 9, ncol = 5)
使用这些rownames
:
rownames(son.mat) <- c("a1","a2","b","a3,a4","c","d1","e1","d2,e2,e3","f")
因此,rownames
中的一些是逗号连接的个别名称。
我有一个data.frame
,可以将各个名称映射到其父名称:
df <- data.frame(son.ids = c("a1","a2","a3","a4","b","c","d1","d2","e1","e2","e3","f"), parent.ids = c("A","A","A","A","B","C","D","D","E","E","E","F"))
我正在寻找的是一种高效方式来创建一个矩阵,该矩阵根据父母的方式组合所有子行。这将涵盖父母只有一个儿子的情况,几个逗号连接的儿子只有一个父母的情况,以及逗号连接的儿子有几个父母的情况。为了更清楚地了解最后一种情况 - 如果son.mat具有包含两个不同父母的son.ids的行(在此示例中&#34; d2,e2,e3和#34;,则表示&#34; D&#34;并且&#34; E&#34;不是分开的,因此所有仅支持&#34; D&#34;和/或仅&#34; E&#34;的行应与支持&#34;的那些行组合在一起。 d,E&#34;
更好地举例说明,输出矩阵将是:
parent.mat <- rbind(apply(son.mat[c(1,2,4),], 2, sum), son.mat[3,], son.mat[5,], apply(son.mat[c(6,7,8),], 2, sum), son.mat[9,])
使用这些rownames
:
rownames(parent.mat) = c("A","B","C","D,E","F")