Let's say I create a data frame in R like this:
df1 <- data.frame(time = 1:20, trial = c(NA, NA, NA, 1, NA, NA, NA, 1, NA, NA, NA, 2, NA, NA, NA, 2, NA, NA, NA, NA))
which outputs:
time trial
1 1 NA
2 2 NA
3 3 NA
4 4 1
5 5 NA
6 6 NA
7 7 NA
8 8 1
9 9 NA
10 10 NA
11 11 NA
12 12 2
13 13 NA
14 14 NA
15 15 NA
16 16 NA
17 17 NA
18 18 2
19 19 NA
20 20 NA
Is there a way I can make the NA's between the 1's also 1, the NA's between the 2's also 2, and the remaining NA's be 0? Essentially, I want the final data frame to look like this:
time trial
1 1 0
2 2 0
3 3 0
4 4 1
5 5 1
6 6 1
7 7 1
8 8 1
9 9 0
10 10 0
11 11 0
12 12 2
13 13 2
14 14 2
15 15 2
16 16 2
17 17 2
18 18 2
19 19 0
20 20 0