首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在多列上使用Group_By()对其余列进行求和

如何在多列上使用Group_By()对其余列进行求和
EN

Stack Overflow用户
提问于 2022-08-04 19:30:37
回答 2查看 58关注 0票数 0

这与Using group by on multiple columns类似,但我不太确定如何将其应用于我的情况。

这里是我的数据头,我想要group_by(Date, Participant Code),然后把所有其他列加起来。

代码语言:javascript
复制
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列分组

代码语言:javascript
复制
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))
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-08-04 19:40:59

我不能用任何简短的代码方式来考虑,但作为临时解决方案,您可以指定摘要中的所有列,如下所示:

代码语言:javascript
复制
df <- df %>%
  group_by(Date, `Participant Code`) %>%
  summarise(time_bike = sum(time_bike),
            distance = sum(distance),
            bike_calories = sum(bike_calories))
票数 1
EN

Stack Overflow用户

发布于 2022-08-04 19:46:47

以下是我想出的:

代码语言:javascript
复制
all_ergo <- all_ergo %>% group_by(`Date`, `Participant Code`) %>% mutate_if(is.character,as.numeric) %>% summarise(across(time_bike:total_calories,sum))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73241366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档