簡體   English   中英

dplyr 使用累積方法按組匯總

[英]dplyr summary by group using cumulative approach

我有一個這樣的data.frame

dat <- data.frame(id = rep(1:4, each = 4),
                  x = 1:16,
                  y = 16:1)

library(dplyr)

我想為每個id做以下操作

for id 1, do mean(x)/mean(y), 
for id 2, do mean(x)/mean(y) where x and y includes values from id 1 and 2 
for id 3, do mean(x)/mean(y) where x and y includes values from id 1, 2 and 3 
for id 4, do mean(x)/mean(y) where x and y includes values from id 1, 2, 3 and 4 

我做了一個傳統的 for 循環來做到這一點

temp.vec <- list()
for(l in sort(unique(dat$id))){
  
  temp.vec[[l]] <- dat %>% 
                   dplyr::filter(id <= l) %>%
                   dplyr::summarise(value = mean(x)/mean(y)) 
  print(l)
}

result <- rbindlist(temp.vec)
result 
value
1: 0.1724138
2: 0.3600000
3: 0.6190476
4: 1.0000000

我可以使用 dplyr 執行此操作嗎?

dat %>%
  group_by(id) %>%
  summarise(mean_x = mean(x), mean_y = mean(y)) %>%
  mutate(result = cumsum(mean_x) / cumsum(mean_y)) %>%
  pluck("result")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM