簡體   English   中英

R 如何在初始 group_by 后總結兩個不同的組

[英]R How to summarize two different groups after initial group_by

我想一次性完成以下操作,而不是產生兩個不同的結果並進行聯合:

delivery_stats= data.frame(service=c("UberEats", "Seamless","UberEats", "Seamless"),
                            status = c("OnTime", "OnTime", "Late", "Late"),
                            totals = c(235, 488, 32, 58))   

ds1 = filter(delivery_stats, service =="UberEats") %>%
         group_by(service, status) %>% 
         summarise(count_status = sum(totals)) %>%
         mutate(avg_of_status = count_status/sum(count_status))

#now do the same for Seamless, then union...

如果我理解正確,你是這個意思嗎?

delivery_stats %>%
    group_by(service) %>%
    mutate(n = sum(totals)) %>%
    transmute(
        status,
        count_status = totals,
        avg_of_status = count_status/n)
## A tibble: 4 x 4
## Groups:   service, status [4]
#  service  status count_status avg_of_status
#  <fct>    <fct>         <dbl>         <dbl>
#1 UberEats OnTime          235         0.880
#2 Seamless OnTime          488         0.894
#3 UberEats Late             32         0.120
#4 Seamless Late             58         0.106

說明:由第一組service來計算的總和totalsservice ; 然后按servicestatus分組以計算count_status = totals的平均值(跨service )。

您也嘗試使用基礎R ave的幫助下within

res <- within(delivery_stats, {
  count_status <- ave(totals, service, status, FUN=mean)
  avg_of_status <- count_status / ave(totals, service, FUN=sum)
})
res
#    service status totals avg_of_status count_status
# 1 UberEats OnTime    235     0.8801498          235
# 2 Seamless OnTime    488     0.8937729          488
# 3 UberEats   Late     32     0.1198502           32
# 4 Seamless   Late     58     0.1062271           58

如上所述,我不必過濾,它對兩個組都可以正常工作:

delivery_stats= data.frame(service=c("UberEats", "Seamless","UberEats", "Seamless"),
                            status = c("OnTime", "OnTime", "Late", "Late"),
                            totals = c(235, 488, 32, 58))



ds1 =    group_by(delivery_stats, service, status) %>% 
         summarise(count_status = sum(totals)) %>%
         mutate(avg_of_status = count_status/sum(count_status))

# A tibble: 4 x 4
# Groups:   service [2]
  service  status count_status avg_of_status
  <fct>    <fct>         <dbl>         <dbl>
1 Seamless Late             58         0.106
2 Seamless OnTime          488         0.894
3 UberEats Late             32         0.120
4 UberEats OnTime          235         0.880

暫無
暫無

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

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