Say I have df:
df <- data.frame(flag = c(rep(0, 20)),
include = c(rep(1, 20)))
df[c(4,8,16), ]$flag <- 1
df
flag include
1 0 1
2 0 1
3 0 1
4 1 1
5 0 1
6 0 1
7 0 1
8 1 1
9 0 1
10 0 1
11 0 1
12 0 1
13 0 1
14 0 1
15 0 1
16 1 1
17 0 1
18 0 1
19 0 1
20 0 1
I want to change the flag include
to 0 if the line is within +/- two lines of the line, where flag == 1
. The result will look like this:
flag include
1 0 1
2 0 0
3 0 0
4 1 1
5 0 0
6 0 0
7 0 0
8 1 1
9 0 0
10 0 0
11 0 1
12 0 1
13 0 1
14 0 0
15 0 0
16 1 1
17 0 0
18 0 0
19 0 1
20 0 1
I thought of some โinnovativeโ (read: inefficient and complicated) ways to do this, but I thought that there should be an easy way with which I skip.
It would be nice if the answer was such that I could generalize it to +/- lines n
, since I have a lot more data and you will look for a potential search within +/- 10 lines ...
source
share