Rilcon42 Rilcon42 - 2 months ago 13
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.



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.