首页 > 解决方案 > Combine rows based on ranges in a column

问题描述

I have a pretty large dataset where I have a column for time in seconds and I want to combine rows where the time is close (range: .1-.2 seconds apart) as a mean.

Here is an example of how the data looks:

BPM seconds
63.9 61.899
63.9 61.902
63.8 61.910
62.1 130.94
62.1 130.95
61.8 211.59
63.8 280.5
60.3 290.4  

So I would want to combine the first 3 rows, then the 2 following after that, and the rest would stand alone. Meaning I would want the data to look like this:

BPM seconds
63.9 61.904
62.1 130.95
61.8 211.59
63.8 280.5
60.3 290.4 

标签: r

解决方案


我们需要创建组,这是重要的一点,其余的是标准聚合:

cumsum(!c(0, diff(df1$seconds)) < 0.2)
# [1] 0 0 0 1 1 2 3 4

然后使用聚合聚合

aggregate(df1[, 2], list(cumsum(!c(0, diff(df1$seconds)) < 0.2)), mean)
#   Group.1         x
# 1       0  61.90367
# 2       1 130.94500
# 3       2 211.59000
# 4       3 280.50000
# 5       4 290.40000

或使用dplyr

library(dplyr)

df1 %>% 
  group_by(myGroup = cumsum(!c(0, diff(seconds)) < 0.2)) %>% 
  summarise(BPM = first(BPM),
            seconds = mean(seconds))
# # A tibble: 5 x 3
#   myGroup   BPM seconds
#     <int> <dbl>   <dbl>
# 1       0  63.9    61.9
# 2       1  62.1   131. 
# 3       2  61.8   212. 
# 4       3  63.8   280. 
# 5       4  60.3   290. 

可重现的示例数据:

df1 <- read.table(text = "BPM seconds
                  63.9 61.899
                  63.9 61.902
                  63.8 61.910
                  62.1 130.94
                  62.1 130.95
                  61.8 211.59
                  63.8 280.5
                  60.3 290.4", header = TRUE)

推荐阅读