这与Using group by on multiple columns类似,但我不太确定如何将其应用于我的情况。
这里是我的数据头,我想要group_by(Date, Participant Code),然后把所有其他列加起来。
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这里我使用了类似的代码,但我不知道如何将其扩展为按2列分组
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))发布于 2022-08-04 19:40:59
我不能用任何简短的代码方式来考虑,但作为临时解决方案,您可以指定摘要中的所有列,如下所示:
df <- df %>%
group_by(Date, `Participant Code`) %>%
summarise(time_bike = sum(time_bike),
distance = sum(distance),
bike_calories = sum(bike_calories))发布于 2022-08-04 19:46:47
以下是我想出的:
all_ergo <- all_ergo %>% group_by(`Date`, `Participant Code`) %>% mutate_if(is.character,as.numeric) %>% summarise(across(time_bike:total_calories,sum))https://stackoverflow.com/questions/73241366
复制相似问题