通过独特的组合扩展数据框并计算它们的均值

问题描述 投票:0回答:2

我正在尝试将行附加到具有现有类组合的数据集。然后我想计算独特类组合的均值。它类似于成对的事后测试,但我想将其他列保留在我的数据集中。

示例数据集:

班级 价值 治疗
A 1 1
2 1
c 3 1

我想要的结果:

班级 平均 治疗
A 1 1
2 1
c 3 1
AB 1.5 1
空调 2 1
ABC 3 1
公元前 2.5 1

我试过 combn 但它没有给我想要的数据表。我正在尝试获得类似于 HSD.test 的输出,但不会丢失我的数据集的其余部分。我能以某种方式使用 expand.grid 函数吗?感谢您的帮助

r combinations
2个回答
3
投票

你可以像这样使用

combn

do.call(c, lapply(seq_along(df$Class), \(m) combn(df$Class, m, FUN = \(x) paste(x, collapse = ""))))
#[1] "A"   "B"   "C"   "AB"  "AC"  "BC"  "ABC"

do.call(c, lapply(seq_along(df$Class), \(m) combn(df$Class, m, FUN = \(x) mean(df$Value[match(x, df$Class)])))
#[1] 1.0 2.0 3.0 1.5 2.0 2.5 2.0

dplyr
框架中:

library(dplyr)
df %>% 
  reframe(Mean = do.call(c, lapply(seq_along(Class), \(m) combn(Class, m, FUN = \(x) mean(Value[match(x, Class)])))), 
          Class = do.call(c, lapply(seq_along(Class), \(m) combn(Class, m, FUN = \(x) paste(x, collapse = "")))), 
          Treatment = 1) %>% 
  select(Class, Mean, Treatment)

#   Class Mean Treatment
# 1     A  1.0         1
# 2     B  2.0         1
# 3     C  3.0         1
# 4    AB  1.5         1
# 5    AC  2.0         1
# 6    BC  2.5         1
# 7   ABC  2.0         1

2
投票

这是第一步,代码不可推广:

librarfy(dplyr)
library(tidyr)
library(purrr)

library(purrr)
library(dplyr)

combos <- combn(unique(df$Class), 2, simplify = FALSE) # get all 2-combos
combos1 <- c(combos, list(unique(df$Class))) # add unique Class combo

result <- map_df(combos1, ~ data.frame(Class = paste0(.x, collapse = ""), stringsAsFactors = FALSE))


bind_rows(df, result) %>% 
  mutate(Value = case_when(
    Class == "AB" ~ (Value[Class=="A"]+ Value[Class=="B"])/2,
    Class == "AC" ~ (Value[Class=="A"]+ Value[Class=="C"])/2,
    Class == "BC" ~ (Value[Class=="B"]+ Value[Class=="C"])/2,
    Class == "ABC" ~ (Value[Class=="A"]+Value[Class=="B"]+ Value[Class=="C"])/2,
    TRUE ~ Value
  )) %>% 
  fill(Treatment, .direction = "down")
 Class Value Treatment
1     A   1.0         1
2     B   2.0         1
3     C   3.0         1
4    AB   1.5         1
5    AC   2.0         1
6    BC   2.5         1
7   ABC   3.0         1
© www.soinside.com 2019 - 2024. All rights reserved.