Rilcon42 Rilcon42 - 1 year ago 79
R Question

What is the right way to reference part of a dataframe after piping?

What is the correct way to do something like this? I am trying to get the

of each group for specific columns. The
syntax seems incorrect with this type of subsetting.


Answer Source

This can be done with summarise_each or in the recent version additional functions like summarise_at, summarise_if were introduced for convenient use.

csv %>%
    group_by(id_num) %>%

csv %>%
     group_by(id_num) %>%
     summarise_at(2:3, sum) 

If we are using column names, wrap it with var in the summarise_at

csv %>%
    group_by(id_num) %>%
    summarise_at(names(csv)[-1], sum)

NOTE: In the OP's dataset, the column names for the 2nd and 3rd columns were not specified resulting in something like c.1..2..3..4..5.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download