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

R语言按Region和Age分组新增Part time行 计算Any减Full time差值

R语言分组显式计算新增行实现方案

需求说明

现有数据集字段为Region、Age、Student Type、Values,原始数据如下:

RegionAgeStudent TypeValues
A17Any32
A17Full time24
A18Any27
A18Full time19
B17Any22
B17Full time14
B18Any80
B18Full time75

需要实现以下逻辑:

  • 按Region、Age分组,每组新增1行数据
  • 新增行Student Type取值为Part time
  • 新增行Values取值为同组内Student Type = "Any"的数值减去Student Type = "Full time"的数值
  • 禁止依赖行顺序使用lag()等位置类函数,需显式匹配类型取值计算,避免原始数据行顺序变化导致计算错误

期望输出结果如下:

RegionAgeStudent TypeValues
A17Any32
A17Full time24
A17Part time8
A18Any27
A18Full time19
A18Part time8
B17Any22
B17Full time14
B17Part time8
B18Any80
B18Full time75
B18Part time5

实现代码

tidyverse方案(推荐,逻辑清晰易读)

该方案完全通过字段值匹配计算,和原始数据行顺序无关:

library(tidyverse)

# 读取/构造原始数据,替换为你自己的数据集读取代码即可
df <- tibble(
  Region = rep(c("A", "B"), each = 4),
  Age = rep(c(17,17,18,18), 2),
  `Student Type` = rep(c("Any", "Full time"), 4),
  Values = c(32,24,27,19,22,14,80,75)
)

# 核心计算
result <- df %>% 
  group_by(Region, Age) %>% 
  # 显式匹配Student Type取值计算Part time值,不受行顺序影响
  summarise(
    `Student Type` = "Part time",
    Values = Values[`Student Type` == "Any"] - Values[`Student Type` == "Full time"],
    .groups = "drop"
  ) %>% 
  # 合并原始数据与新增行
  bind_rows(df, .) %>% 
  # 按要求排序输出
  arrange(Region, Age, factor(`Student Type`, levels = c("Any", "Full time", "Part time")))

基础R方案(无需加载第三方包)

# 按分组拆分数据计算Part time行
part_time_rows <- do.call(rbind, lapply(split(df, ~Region + Age), function(g) {
  data.frame(
    Region = g$Region[1],
    Age = g$Age[1],
    `Student Type` = "Part time",
    Values = g$Values[g$`Student Type` == "Any"] - g$Values[g$`Student Type` == "Full time"],
    check.names = FALSE
  )
}))

# 合并数据并排序
result <- rbind(df, part_time_rows, make.row.names = FALSE)
result <- result[order(
  result$Region, 
  result$Age, 
  factor(result$`Student Type`, levels = c("Any", "Full time", "Part time"))
), ]

两种方案均直接通过Student Type的取值定位对应数值,不依赖行的先后顺序,哪怕原始数据同组内Any、Full time行顺序颠倒,计算结果也不会出错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 17:06:26