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

如何在R语言中结合group_by、ifelse与filter处理数据框

Solution for Grouped Filtering in R

Got it, let's work through this problem together. The goal is to keep entire groups only if the row with result > 1 falls exactly on the maximum month of that group. Here's a clean, readable way to do it with dplyr functions:

Full Code

library(dplyr)

# Your sample data
dat <- data.frame(x = c("A","A","A","A","A","B","B","B","B","B"), 
                  month = c(1,2,3,4,5,1,2,3,4,5), 
                  result = c(.5,.6,1.2,1.1,.9,.3,.4,.5,.9,1.2))

# Filter groups based on your criteria
dat_filtered <- dat %>%
  group_by(x) %>%
  mutate(
    # Calculate the maximum month for each group
    group_max_month = max(month),
    # Check if ANY row in the group meets both conditions: result >1 AND month is group max
    should_keep = any(result > 1 & month == group_max_month)
  ) %>%
  filter(should_keep) %>%
  # Remove helper columns (optional, clean up the output)
  select(-group_max_month, -should_keep) %>%
  ungroup()

# View the result
dat_filtered

Breakdown of Each Step

  • group_by(x): First, we split the data into groups based on the x column—all operations after this will apply per group.
  • mutate() creates two helper columns:
    • group_max_month: Captures the latest month for each group, so we can check if the result >1 row lines up with it.
    • should_keep: Uses any() to check if there's at least one row in the group where result >1 and that row's month is the group's maximum. If yes, the whole group gets marked to keep (TRUE); if no, it's marked to discard (FALSE).
  • filter(should_keep): Keeps only the groups where should_keep is TRUE—this retains all rows in qualifying groups, just like you wanted.
  • select(...) and ungroup(): Clean up the helper columns and return the data to an ungrouped state for future operations.

Output Verification

When you run this code, you'll see only the B group is retained (since its result >1 is in month 5, which is the group's max month). The A group gets dropped because its result >1 rows are in months 3 and 4—not the max month 5.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:55:11