mercredi 24 avril 2019

Create new column when when values repeat 3 or more times

Problem

I'm trying to create a new column (b) based on values from a previous column (a). Column a is binary, consisting of either 0's or 1's. If there are three or more 1's in a row in column a, then keep them in column b. I'm close to the desired output, but when there are two 1's in a row, the ifelse grabs the second value because it's meeting the first condition.

Desired Output–Column b

df <- data.frame(a = c(1,1,1,0,0,1,0,1,1,0,1,1,1,0,1,1,0,1,1,1,1),
                 b = c(1,1,1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,1))
df
   a b
1  1 1
2  1 1
3  1 1
4  0 0
5  0 0
6  1 0
7  0 0
8  1 0 #
9  1 0 # 
10 0 0
11 1 1
12 1 1
13 1 1
14 0 0
15 1 0 #
16 1 0 #
17 0 0
18 1 1
19 1 1
20 1 1
21 1 1

Failed Attempt...s

require(dplyr)
df_fail <- df %>% mutate(b=ifelse((lag(df$a) + df$a) > 1 |(df$a + lead(df$a) + lead(df$a,2)) >= 3, df$a,NA))

df_fail
   a b
1  1 1
2  1 1
3  1 1
4  0 0
5  0 0
6  1 0
7  0 0
8  1 0
9  1 1 # should be 0
10 0 0
11 1 1
12 1 1
13 1 1
14 0 0
15 1 0
16 1 1 # should be 0
17 0 0
18 1 1
19 1 1
20 1 1
21 1 1

Aucun commentaire:

Enregistrer un commentaire