我有一个对称矩阵/数据框,看起来像
structure(list(HQ673618_1 = c(NA, 90.8, 89.8, 89.6, 89.8, 88.9,
87.8, 88.2, 88.3), HQ674317_1 = c(90.8, NA, 98.6, 97.7, 98.4,
97.4, 94.9, 96.2, 95.1), EU686630_1 = c(89.8, 98.6, NA, 98.4,
98.9, 97.7, 95.4, 96.4, 95.8), EU686593_2 = c(89.6, 97.7, 98.4,
NA, 98.1, 96.8, 94.4, 95.6, 94.8), JN166322_2 = c(89.8, 98.4,
98.9, 98.1, NA, 97.5, 95.3, 96.5, 95.9), EU491340_2 = c(88.9,
97.4, 97.7, 96.8, 97.5, NA, 96.5, 97.7, 96), AB694259_3 = c(87.8,
94.9, 95.4, 94.4, 95.3, 96.5, NA, 98.3, 95.9), AB694258_3 = c(88.2,
96.2, 96.4, 95.6, 96.5, 97.7, 98.3, NA, 95.8), AB694462_3 = c(88.3,
95.1, 95.8, 94.8, 95.9, 96, 95.9, 95.8, NA)), .Names = c("HQ673618_1",
"HQ674317_1", "EU686630_1", "EU686593_2", "JN166322_2", "EU491340_2",
"AB694259_3", "AB694258_3", "AB694462_3"), class = "data.frame", row.names = c("HQ673618_1",
"HQ674317_1", "EU686630_1", "EU686593_2", "JN166322_2", "EU491340_2",
"AB694259_3", "AB694258_3", "AB694462_3"))
这些值代表样本的相似性。 在第一步中,id想知道“_n”指定的每个处理的平均值:
困难在于让R知道类别因子在行/列名中给出。此外,我的数据集比示例大得多,并且样本大小在每个处理中都有所不同。
感谢您的支持。
答案 0 :(得分:2)
这是一个可能的解决方案(假设您的数据集名为df
)
indx <- gsub(".*_", "", names(df))
vapply(unique(indx), function(x) {
temp <- which(indx %in% x)
mean(unlist(df[temp, temp]), na.rm = TRUE)
},
FUN.VALUE = double(1))
# 1 2 3
# 93.06667 97.46667 96.66667