user702846 user702846 - 1 year ago 145
R Question

dpylr how to summarize given multilevel grouping

I have a dataframe like

count sample type
1 DLF002 a
1 DLF002 a
1 DLF002 b
3 DLF001 a
3 DLF001 b
3 DLF001 b


and would like to make a summary given two factor (sample and type), so I would like to have a result like the following

count sample type
2 DLF002 a
1 DLF002 b
3 DLF001 a
6 DLF001 b


I have tried something like,

group_by(df, sample) %>% summarize(sx = sum(count)),


but this does not into account the "type"; I wonder how I can make this two level grouping, using dpylr

Answer Source

Simply add multiple grouping variables to group_by:

group_by(df, sample, type) %>% summarize(sx = sum(count))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download