I have a data frame with several columns. Based on the column 'activity', I want to remove entire contiguous runs of a specific value, 'pt', but only when they occur immediately before or after a run of 'outside'.
In the simplified data below, there is one run where 'activity' is 'outside', and which have chunks of 'pt' before and after. These two 'pt' chunks should be removed.
activity dist
1 home 1
2 pt 2 # <- run of 'pt' before run of 'outside': remove
3 pt 3 # <-
4 pt 4 # <-
5 outside 5
6 outside 6
7 pt 7 # <- run of 'pt' after run of 'outside': remove
8 pt 8 # <-
9 work 9
10 pt 10
11 pt 11
12 home 12
Thus, the desired output is:
activity dist
1 home 1
2 outside 5
3 outside 6
4 work 9
5 pt 10
6 pt 11
7 home 12
How can this be achieved?
dput of data:
structure(list(activity = c("home", "pt", "pt", "pt", "outside", "outside", "pt", "pt", "work", "pt", "pt", "home"),
dist = 1:12),
class = "data.frame", row.names = c(NA, -12L))