最小值和最大值基于另一列,并将它们组合在r中

时间:2019-09-10 11:55:00

标签: r max conditional-statements min

因此,我基本上得到了一个while循环函数,该函数基于“百分比”列中的最高百分比在“ algorithm_column”中创建1,直到达到某个总百分比(90%左右)。其余未考虑的行在“ algorithm_column”(Create while loop function that takes next largest value untill condition is met)中的值为0

我想根据找到的循环函数显示“ timeinterval”列的最小和最大时间(最小值是1的开始位置,最大值是带有1的最后一行,0不在其中范围)。然后最终从中创建一个时间间隔。

因此,如果我们有以下代码,我想在另一列中创建,假设“ total_time”是从最小时间09:00(这是从algorithm_column中1开始的地方)到11:15的计算,在“ total_time”列中添加了02:15小时的时间间隔。

algorithm
#    pc4 timeinterval stops percent idgroup algorithm_column
#1  5464     08:45:00     1  1.3889       1                0
#2  5464     09:00:00     5  6.9444       2                1
#3  5464     09:15:00     8 11.1111       3                1
#4  5464     09:30:00     7  9.7222       4                1
#5  5464     09:45:00     5  6.9444       5                1
#6  5464     10:00:00    10 13.8889       6                1
#7  5464     10:15:00     6  8.3333       7                1
#8  5464     10:30:00     4  5.5556       8                1
#9  5464     10:45:00     7  9.7222       9                1
#10 5464     11:00:00     6  8.3333      10                1
#11 5464     11:15:00     5  6.9444      11                1
#12 5464     11:30:00     8 11.1111      12                0

我有多个pc4组,因此它应该查看每个组并分别计算每个组的total_time。

我有此功能,但是如果需要的话,我有点卡住了。

test <- function(x) {
  ind <- x[["algorithm$algorithm_column"]] == 0
  Mx <- max(x[["timeinterval"]][ind], na.rm = TRUE);
  ind <- x[["algorithm$algorithm_column"]] == 1
  Mn <- min(x[["timeinterval"]][ind], na.rm = TRUE);
  list(Mn, Mx)  ## or return(list(Mn, Mx))
}

test(algorithm)

1 个答案:

答案 0 :(得分:0)

这是一个dplyr解决方案。

library(dplyr)

algorithm %>%
  mutate(tmp = cumsum(c(0, diff(algorithm_column) != 0))) %>%
  filter(algorithm_column == 1) %>%
  group_by(pc4, tmp) %>%
  summarise(first = first(timeinterval),
            last = last(timeinterval)) %>%
  select(-tmp)
## A tibble: 1 x 3
## Groups:   pc4 [1]
#    pc4 first    last    
#  <int> <fct>    <fct>   
#1  5464 09:00:00 11:15:00

数据。

algorithm <- read.table(text = "
    pc4 timeinterval stops percent idgroup algorithm_column
1  5464     08:45:00     1  1.3889       1                0
2  5464     09:00:00     5  6.9444       2                1
3  5464     09:15:00     8 11.1111       3                1
4  5464     09:30:00     7  9.7222       4                1
5  5464     09:45:00     5  6.9444       5                1
6  5464     10:00:00    10 13.8889       6                1
7  5464     10:15:00     6  8.3333       7                1
8  5464     10:30:00     4  5.5556       8                1
9  5464     10:45:00     7  9.7222       9                1
10 5464     11:00:00     6  8.3333      10                1
11 5464     11:15:00     5  6.9444      11                1
12 5464     11:30:00     8 11.1111      12                0
", header = TRUE)