汇总dplyr中每组的所有其他值

时间:2019-10-20 20:37:14

标签: r dplyr

我有一个数据集,其中的每个决策都是按组进行的。对于每个人,我需要他/她的小组成员的所有决定的汇总(即总和)。 假设数据看起来像这样:

set.seed(123)
group_id <- c(sapply(seq(1, 3), rep, times = 3))
person_id <- rep(seq(1,3),3)
decision <- sample(1:10, 9, replace=T)
df <-data.frame(group_id, person_id, decision)
df

结果是:

  group_id person_id decision
1        1         1        3
2        1         2        8
3        1         3        5
4        2         1        9
5        2         2       10
6        2         3        1
7        3         1        6
8        3         2        9
9        3         3        6

我需要产生类似的东西:

  group_id person_id decision others_decision
1        1         1        3 13
2        1         2        8  8
3        1         3        5 11

因此,对于组中的每个元素,我都得到了同一组中的所有其他成员,并做了一些事(求和)。我可以仅使用for循环来完成此操作,但这看起来很丑陋且效率低下。有更好的解决方案吗?

更新:

这是我到目前为止找到的解决方案,很抱歉:

df$other_decision=unlist(by(df, 1:nrow(df), function(row) {
  df %>% filter(group_id==row$group_id, person_id!=row$person_id) %>% summarize(sum(decision))
}
  ))
df

3 个答案:

答案 0 :(得分:4)

您可以这样做:

df %>%
 inner_join(df, by = c("group_id" = "group_id")) %>%
 filter(person_id.x != person_id.y) %>%
 group_by(group_id, person_id = person_id.x) %>%
 summarise(decision = first(decision.x),
           others_decison = sum(decision.y))

  group_id person_id decision others_decison
     <int>     <int>    <int>          <int>
1        1         1        3             13
2        1         2        8              8
3        1         3        5             11
4        2         1        9             11
5        2         2       10             10
6        2         3        1             19
7        3         1        6             15
8        3         2        9             12
9        3         3        6             15

取决于您的实际数据集(其大小),由于涉及内部联接,因此在计算上可能会变得相当苛刻。

不涉及内部联接的另一种可能性是:

df %>% 
 group_by(group_id) %>% 
 mutate(others_decison = list(decision),
        rowid = 1:n()) %>%
 ungroup() %>%
 rowwise() %>%
 mutate(others_decison = sum(unlist(others_decison)[-rowid])) %>%
 ungroup() %>%
 select(-rowid)

答案 1 :(得分:3)

这可以通过创建一个将函数作为参数并从依次传递给它的向量中删除每个观察值的函数来相当简单地完成。

library(dplyr)

my_summarise <- function(x, FUN, ...) {
  sapply(seq_along(x), function(y)
    FUN(x[-y], ...))
} 

df %>%
  group_by(group_id) %>%
  mutate(dsum = my_summarise(decision, sum),
         dmean = my_summarise(decision, mean),
         dmax = my_summarise(decision, max))

# A tibble: 9 x 6
# Groups:   group_id [3]
  group_id person_id decision  dsum dmean  dmax
     <int>     <int>    <int> <int> <dbl> <int>
1        1         1        3    13   6.5     8
2        1         2        8     8   4       5
3        1         3        5    11   5.5     8
4        2         1        9    11   5.5    10
5        2         2       10    10   5       9
6        2         3        1    19   9.5    10
7        3         1        6    15   7.5     9
8        3         2        9    12   6       6
9        3         3        6    15   7.5     9

答案 2 :(得分:2)

以下是一些方法:

library(data.table)
dt <- as.data.table(df)

# don't update original dt
dt[dt, on = .(group_id), allow.cartesian = T
   ][person_id != i.person_id,
     .(decison = first(i.decision), others = sum(decision)),
     by = .(group_id, person_id = i.person_id)]

#update the original dt way 1
dt[,
   others_decision := .SD[.SD, on = .(group_id), allow.cartesian = T
                          ][person_id != i.person_id, sum(decision), by = .(group_id,i.person_id)]$V1
   ]

#update the original dt way 2
dt1[, 
   others_decision := dt[group_id == .BY[[1]] & person_id != .BY[[2]], sum(decision)],
   by = .(group_id, person_id)]

前两个主要方面或多或少是@tmfmnk的方法,但是通过data.table。最后一个对我来说更直观,但可能是最慢的。