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

如何高效合并R数据框中值相近的行并计算均值?

Great question! Using vectorized operations instead of a for loop will be way more efficient, especially for larger datasets. Here's a clean, optimized solution using dplyr:

library(dplyr)

DF <- data.frame(ID=letters[1:12], Values=c(1, 2.2, 3, 5, 6.2, 6.8, 7, 8.5, 10, 12.2, 13, 14))

result <- DF %>%
  # Create a group identifier: start new group when adjacent Values differ by >=1
  mutate(group = cumsum(c(TRUE, diff(Values) >= 1))) %>%
  # Group by our new group column
  group_by(group) %>%
  # Summarize: concatenate IDs and calculate mean Values (rounded to 2 decimals)
  summarize(
    ID = paste(ID, collapse = "/"),
    Values = round(mean(Values), 2)
  ) %>%
  # Remove the group column since we don't need it in the final output
  select(-group)

print(result)

Output:

# A tibble: 8 × 2
  ID     Values
  <chr>   <dbl>
1 a        1.00
2 b/c      2.60
3 d        5.00
4 e/f/g    6.67
5 h        8.50
6 i       10.00
7 j/k     12.60
8 l       14.00

How this works:

  1. Group Identification: The cumsum(c(TRUE, diff(Values) >=1)) creates a unique group number for each set of consecutive rows where adjacent Values differ by less than 1. The TRUE ensures the first row starts a new group, and every time the difference between the current and previous value is ≥1, we increment the group number.
  2. Summarization: Using group_by(group) lets us aggregate each group:
    • paste(ID, collapse="/") concatenates all IDs in the group with slashes.
    • round(mean(Values),2) calculates the average of the Values in the group and rounds it to 2 decimal places (matching your example).
  3. Efficiency: This approach uses vectorized operations (no loops) which are optimized in R, making it much faster than a for loop—especially as your dataframe grows in size.

Note: I fixed a small typo in your expected output (the last row's ID should be l instead of i, since your original dataframe uses letters[1:12]).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:22:29