birdsdidsing
birdsdidsing

Reputation: 57

How to summarize data by several groups (R)

I have a dataframe that looks like this:

enter image description here

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):

enter image description here

Can someone help? I have seen other issues (like this: Summarizing by group and subgroup) which are similar but not for R.

Upvotes: 0

Views: 32

Answers (1)

akrun
akrun

Reputation: 887088

We can do

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

Upvotes: 1

Related Questions