I'm having a rough time isolating n-rows before and after a flag by group
I found an answer elsewhere that sort of worked, but was thrown off by groups with less than the scope of rows. For example if the scope was 6 rows but a group only had five observations the query would start including irrelevant observations from a prior group.
Here's some dummy data to reproduce.
x <- c("", "", "", "1", "", "","", "", "", "", "", "1","", "", "", "", "1", "")
y <- c("2", "6", "4", "4", "7", "9","1", "15", "7", "4", "5", "8","6", "1", "2", "4", "6", "16")
z <- c("a", "a", "a", "a", "a", "a","a", "b", "b", "b", "b", "b","b", "b", "c", "c", "c", "c")
a <- as.data.frame(cbind(x, y, z))
x y z
1 2 a
2 6 a
3 4 a
4 1 4 a
5 7 a
6 9 a
7 1 a
8 15 b
9 7 b
10 4 b
11 5 b
12 1 8 b
13 6 b
14 1 b
15 2 c
16 4 c
17 1 6 c
18 16 c
Ideally I'd like to have a
look something like this:
x y z
1 6 a
2 4 a
3 1 4 a
4 7 a
5 9 a
6 1 a
7 4 b
8 5 b
9 1 8 b
10 6 b
11 1 b
12 2 c
13 4 c
14 1 6 c
15 16 c