Srini167 Srini167 - 6 months ago 23
R Question

Aggregate numeric column values for duplicate rowID and retain first occurrence value for non numeric columns

ID Method Sales
1 Call 10
2 Visit 20
3 Call 10
2 Visit 5
5 Call 5
1 Call 10
2 Visit 15


I would like the output to be:

ID Method Sales
1 Call 20
2 Visit 40
3 Call 10
5 Call 5


I am able to aggregate the sales based on ID but not sure how to bring in the Method.

Answer Source

A general solution (per your title) to

  1. sum all numeric variables and
  2. retain the first value of any non-numeric variables:

.

library(dplyr)
df %>% group_by(ID) %>% mutate_if(is.numeric, sum) %>% slice(1)

Gives:

# A tibble: 4 x 3
# Groups:   ID [4]
     ID Method Sales
  <int>  <chr> <int>
1     1   Call    20
2     2  Visit    40
3     3   Call    10
4     5   Call     5
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download