This is similar to Using group by on multiple columns but I'm not too sure how to apply it to my case.
Here is the head of my data
I would like to group_by(Date, Participant Code)
, and then sum all the other columns.
head(all_ergo)
# A tibble: 6 × 10
Date time_bike distance bike_calories power `Participant Code` time_active time_total desk_ca…¹ total…²
<date> <dbl> <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl>
1 2022-04-12 120 0 0 0.00613 AE1_01 360 360 11.0 11.0
2 2022-04-12 120 0 0 0.00613 AE1_01 1920 1920 58.6 58.6
3 2022-04-12 120 0 0 0.00613 AE1_01 3480 3480 106. 106.
4 2022-04-12 120 0 0 0.00613 AE1_01 3540 3540 108. 108.
5 2022-04-12 120 0 0 0.00580 AE1_01 360 360 11.0 11.0
6 2022-04-12 120 0 0 0.00580 AE1_01 1920 1920 58.6 58.6
# … with abbreviated variable names ¹desk_calories, ²total_calories
Here I used a similar code, but I can't figure out how to expand it to grouping by 2 columns
Summary_PRE <- workday_PRE%>% group_by(Date) %>% mutate_if(is.character,as.numeric) %>% summarise(across(Axis1:Counter,sum))
Summary_PRE <- subset (Summary_PRE, select = -c(Axis1,Axis2,Axis3,VM))
CodePudding user response:
I cant think in any short code way, but maybe as a temp solution you can specify all the columns in the summarise, like this:
df <- df %>%
group_by(Date, `Participant Code`) %>%
summarise(time_bike = sum(time_bike),
distance = sum(distance),
bike_calories = sum(bike_calories))
CodePudding user response:
Here is what I came up with:
all_ergo <- all_ergo %>% group_by(`Date`, `Participant Code`) %>% mutate_if(is.character,as.numeric) %>% summarise(across(time_bike:total_calories,sum))