简体   繁体   English

如何按几组汇总数据(R)

[英]How to summarize data by several groups (R)

I have a dataframe that looks like this:我有一个看起来像这样的 dataframe:

在此处输入图像描述

And I want to get this, that is, a single row per Group, with a column for the % of As in all the ID_1_Subgroup for each Group, together with the sum of ValueSubgroup, for each group too):我想得到这个,即每个组有一行,每个组的所有 ID_1_Subgroup 中的 As 百分比列,以及每个组的 ValueSubgroup 的总和):

在此处输入图像描述

Can someone help?有人可以帮忙吗? I have seen other issues (like this: Summarizing by group and subgroup ) which are similar but not for R.我已经看到了其他类似的问题(例如: Summarizing by group and subgroup ),但不适用于 R。

We can do我们可以做的

library(dplyr)
df1 %>%
     group_by(Group) %>%
     summarise(PercA = mean(id_1_Subgroup == "A"),
               SumOfValueSubgroup = sum(ValueSubgroup))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM