I think this will give you the required averages.
dataset %>%
mutate(temp = with(data = rle(x = port),
expr = rep.int(x = seq_along(along.with = values),
times = lengths)),
row_index = row_number()) %>%
group_by(temp) %>%
top_n(n = 3) %>%
summarise(corresponding_port = unique(x = port),
avg_of_last_three = mean(nh3))
I'm trying on the cycle part. Will update this very post (hopefully soon enough)
Edit
Does this help?
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
dataset <- tibble(date = c("28.1.19", "28.1.19", "28.1.19", "28.1.19", "28.1.19",
"28.1.19", "28.1.19", "28.1.19", "28.1.19",
"28.1.19", "28.1.19", "28.1.19", "28.1.19", "28.1.19",
"28.1.19", "28.1.19", "28.1.19", "28.1.19", "28.1.19",
"28.1.19", "28.1.19", "28.1.19", "28.1.19", "28.1.19",
"28.1.19", "28.1.19", "28.1.19", "28.1.19", "28.1.19",
"28.1.19", "28.1.19", "28.1.19", "28.1.19",
"28.1.19", "28.1.19", "28.1.19", "28.1.19", "28.1.19",
"28.1.19", "28.1.19", "28.1.19", "28.1.19", "28.1.19",
"28.1.19", "28.1.19", "28.1.19", "28.1.19", "28.1.19",
"28.1.19", "28.1.19", "28.1.19"),
time = c("09:44:52.657", "09:44:54.206", "09:44:56.012",
"09:44:57.836", "09:44:59.637", "09:45:01.427",
"09:45:03.226", "09:45:05.026", "09:45:06.828",
"09:45:08.637", "09:45:10.457", "09:45:12.269",
"09:45:13.917", "09:45:15.483", "09:45:17.315", "09:45:19.125",
"09:45:20.934", "09:45:22.755", "09:45:24.563",
"09:45:26.378", "09:45:28.193", "09:45:30.013",
"09:45:31.812", "09:45:33.615", "09:45:35.276", "09:45:36.834",
"09:45:38.647", "09:45:40.456", "09:45:42.266",
"09:45:44.065", "09:45:45.875", "09:45:47.703",
"09:45:49.516", "09:45:53.167", "09:45:54.973", "09:45:56.615",
"09:45:58.157", "09:45:59.956", "09:46:01.755",
"09:46:03.585", "09:46:05.408", "09:46:07.232",
"09:46:09.055", "09:46:10.869", "09:46:12.693", "09:46:14.510",
"09:46:16.332", "09:46:18.006", "09:46:19.581",
"09:46:21.395", "09:46:23.214"),
port = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4,
4, 5, 5, 5, 5, 5, 5, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3,
3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5),
nh3 = c(361.9, 358.6, 363.6, 368, 371.8, 374.8, 378.4, 381.8, 386.3,
390.3, 396.3, 399.1, 399.1, 399.2, 398.1, 397.5,
396.1, 394.9, 390.6, 387.1, 384.9, 382.7, 381.8, 380.6,
380.6, 382.2, 385.5, 387, 386.3, 384.6, 382.4,
381.1, 380.5, 375.2, 370.8, 370.8, 413.1, 439.2, 470.7,
473.6, 482.6, 483.1, 506, 504.6, 504, 508.7, 508.5,
508.5, 503.9, 502.4, 494.9))
dataset %>%
mutate(cycle = cumsum(x = (((port == 1) & (lag(x = port) != 1)) | (row_number() == 1))),
row_index = row_number()) %>%
group_by(cycle, port) %>%
top_n(3) %>%
summarise(average_of_last_three_nh3 = mean(x = nh3)) %>%
ungroup()
#> Selecting by row_index
#> # A tibble: 10 x 3
#> cycle port average_of_last_three_nh3
#> <int> <dbl> <dbl>
#> 1 1 1 368.
#> 2 1 2 395.
#> 3 1 3 397.
#> 4 1 4 385.
#> 5 1 5 385.
#> 6 2 1 384.
#> 7 2 2 372.
#> 8 2 3 461.
#> 9 2 4 505.
#> 10 2 5 500.
Created on 2019-08-23 by the reprex package (v0.3.0)