gpt4 book ai didi

r - 合并(平均)具有部分匹配标题名称的列

转载 作者:行者123 更新时间:2023-12-05 00:56:46 28 4
gpt4 key购买 nike

我有一个看起来像的数据:

   AAA_1   AAA_2  AAA_3  BBB_1  BBB_2  BBB_3 CCC
1 1 1 1 2 2 2 1
2 3 1 4 0 0 0 0
3 5 3 0 1 1 1 1

对于每一行,我想为那些具有共同特征的列求平均值,如下所示
feature <- c("AAA","BBB","CCC")

所需的输出应如下所示:
   AAA   BBB   CCC
1 1 2 1
2 2.6 0 0
3 2.6 1 1

对于每个模式,我都能够做到:
data <- read.table("data.txt",header=T,row.name=1)
AAA <- as.matrix(rowMeans(data[ , grepl("AAA" , names( data ) ) ])

但我不知道如何对一行中的不同模式进行部分匹配

还尝试了其他一些事情,例如:
for (i in 1:length(features)){
feature[i] <- as.matrix(rowMeans(data[ , grepl(feature[i] , names( data ) ) ]))
}

最佳答案

假设您的 colnames始终如您的示例所示结构化,然后您可以拆分名称和聚合。

new_names <-  unlist(strsplit(names(df),"\\_.*"))
colnames(df) <- new_names
#Testing with your data, we need to prevent the loss of dimension by using drop = FALSE
sapply(unique(new_names), function(i) rowMeans(df[, new_names==i, drop = FALSE]))
# AAA BBB CCC
#[1,] 1.000000 2 1
#[2,] 2.666667 0 0
#[3,] 2.666667 1 1

数据:
df <- structure(list(AAA_1 = c(1L, 3L, 5L), AAA_2 = c(1L, 1L, 3L), 
AAA_3 = c(1L, 4L, 0L), BBB_1 = c(2L, 0L, 1L), BBB_2 = c(2L,
0L, 1L), BBB_3 = c(2L, 0L, 1L), CCC = c(1L, 0L, 1L)), .Names = c("AAA_1",
"AAA_2", "AAA_3", "BBB_1", "BBB_2", "BBB_3", "CCC"), class = "data.frame", row.names = c(NA,
-3L))

关于r - 合并(平均)具有部分匹配标题名称的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35772979/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com