I have the following data frame:
miniDF1 <- data.frame(Pred = c("A","A","B","A","B","B","C","A","B","C","A","A","A","A","B","A","C","B"))
Pred
1 A
2 A
3 B
4 A
5 B
6 B
7 C
8 A
9 B
10 C
11 A
12 A
13 A
14 A
15 B
16 A
17 C
18 B
I am trying to make a new column filled with 1's until "C" is found in Pred, and then fill the new column with 0's until the next "C" is found, and repeat as such until the end of the DF. I have tried the following:
miniDF1 <- miniDF1 %>%
mutate(Outcome = ifelse(str_detect(Pred, "C"), 1, 0)) %>%
fill(Outcome, .direction = 'up')
Pred Outcome
1 A 0
2 A 0
3 B 0
4 A 0
5 B 0
6 B 0
7 C 1
8 A 0
9 B 0
10 C 1
11 A 0
12 A 0
13 A 0
14 A 0
15 B 0
16 A 0
17 C 1
18 B 0
but this is only putting 1's in the same row where there are "C's" located.
This is how it is expected to look like:
miniDF2 <- data.frame(Pred = c("A","A","B","A","B","B","C","A","B","C","A","A","A","A","B","A","C","B"),
Outcome = c(1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,0))
Pred Outcome
1 A 1
2 A 1
3 B 1
4 A 1
5 B 1
6 B 1
7 C 1
8 A 0
9 B 0
10 C 0
11 A 1
12 A 1
13 A 1
14 A 1
15 B 1
16 A 1
17 C 1
18 B 0
I can't figure out how to get the values to flip accordingly, but I thought that that's what the fill(Outcome, .direction = 'up') part of my code was intended to do.