You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何对R语言data.frame中同类运动列计算负荷并生成总负荷?

处理R语言运动训练数据:计算单运动负荷与总负荷

我有一个R语言的data.frame,包含多种运动类型的相关列,记录了运动所用重量、重复次数和组数。列名前缀对应不同运动(如Bic、Lat、PullUp),后缀统一为Wt(重量)、Sets(组数)、Reps(重复次数)。

需要实现以下需求:

  • 对每种运动,计算Wt * Sets * Reps,将结果保存为后缀为Load的新列(如BicLoad);
  • 将所有后缀为Load的列求和,生成Total_Load列。

示例数据(MWE)

exercise <- data.frame(
  BicWt = c(3, 3, 4),
  BicSets = c(2, 2, 2),
  BicReps = c(10, 12, 12),
  LatWt = c(4, 4, 4),
  LatSets = c(2, 2, 2),
  LatReps = c(6, 8, 10)
)

初始数据展示:

BicWt BicSets BicReps LatWt LatSets LatReps
1     3       2      10     4       2       6
2     3       2      12     4       2       8
3     4       2      12     4       2      10

期望输出

BicWt BicSets BicReps BicLoad LatWt LatSets LatReps LatLoad Total_Load
1     3       2      10      60     4       2       6      48        108
2     3       2      12      72     4       2       8      64        136
3     4       2      12      96     4       2      10      80        176

含多大小写前缀的补充示例

当运动前缀包含大写字母(如PullUp)时:

exercise <- data.frame(
  BicWt = c(3, 3, 4),
  BicSets = c(2, 2, 2),
  BicReps = c(10, 12, 12),
  LatWt = c(4, 4, 4),
  LatSets = c(2, 2, 2),
  LatReps = c(6, 8, 10),
  PullUpWt = c(5, 5, 5),
  PullUpSets = c(2, 2, 2),
  PullUpReps = c(6, 8, 10)
)

对应期望输出

BicWt BicSets BicReps BicLoad LatWt LatSets LatReps LatLoad PullUpWt PullUpSets PullUpReps PullUpLoad Total_Load
1     3       2      10      60     4       2       6      48        5          2          6         60        168
2     3       2      12      72     4       2       8      64        5          2          8         80        216
3     4       2      12      96     4       2      10      80        5          2         10        100        276

解决方案

方法1:基础R实现

无需额外安装包,通过正则提取运动前缀并循环计算:

# 提取唯一的运动前缀
exercise_cols <- colnames(exercise)
exercise_prefixes <- unique(sub("(Wt|Sets|Reps)$", "", exercise_cols))

# 循环计算每个运动的Load列
for(prefix in exercise_prefixes) {
  wt_col <- paste0(prefix, "Wt")
  sets_col <- paste0(prefix, "Sets")
  reps_col <- paste0(prefix, "Reps")
  load_col <- paste0(prefix, "Load")
  exercise[[load_col]] <- exercise[[wt_col]] * exercise[[sets_col]] * exercise[[reps_col]]
}

# 计算总负荷
exercise$Total_Load <- rowSums(exercise[, grepl("Load$", colnames(exercise))])

方法2:tidyverse实现

使用dplyr和tidyr包,通过长宽格式转换实现,代码更简洁:

library(dplyr)
library(tidyr)

exercise_processed <- exercise %>%
  # 转为长格式,拆分运动前缀和指标类型
  pivot_longer(everything(), 
               names_to = c("exercise", ".value"),
               names_pattern = "(.*)(Wt|Sets|Reps)") %>%
  # 计算单运动负荷
  mutate(Load = Wt * Sets * Reps) %>%
  # 转回宽格式
  pivot_wider(names_from = exercise,
              values_from = c(Wt, Sets, Reps, Load),
              names_glue = "{exercise}{.value}") %>%
  # 计算总负荷
  rowwise() %>%
  mutate(Total_Load = sum(c_across(ends_with("Load")))) %>%
  ungroup()

两种方法均支持带大小写的运动前缀,执行后即可得到符合需求的结果。


内容的提问来源于stack exchange,提问作者Mark Davies

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 13:55:16