Reputation: 393
I'm trying to get the index of the nearest TRUE value from a column based on the entry of another column. My sample dataframe goes like this:
a <- c(FALSE,TRUE,FALSE,FALSE,FALSE,TRUE,FALSE,FALSE,TRUE,TRUE,FALSE,FALSE,FALSE,FALSE,FALSE)
b <- c(NA, NA, 3, NA, NA, NA, NA, 8, NA, NA, NA, 12, NA, NA, NA)
df <- data.frame(a, b)
I would like to make a new column that gives the index of the nearest TRUE value before a given value from b
. To illustrate, the resulting df
should look like this:
a b c
1 FALSE NA NA
2 TRUE NA NA
3 FALSE 3 2
4 FALSE NA NA
5 FALSE NA NA
6 TRUE NA NA
7 FALSE NA NA
8 FALSE 8 6
9 TRUE NA NA
10 TRUE NA NA
11 FALSE NA NA
12 FALSE 12 10
13 FALSE NA NA
14 FALSE NA NA
15 FALSE NA NA
I know I can use max(which(df$a == TRUE))
but I don't know how to make it work in such a way that it only considers the index of the preceding values. Thanks!
Upvotes: 2
Views: 101
Reputation: 388982
We can get the index of all TRUE
values and then use findInterval
to get the closest one for each value in b
.
inds <- which(df$a)
df$c <- inds[findInterval(df$b, inds)]
df
# a b c
#1 FALSE NA NA
#2 TRUE NA NA
#3 FALSE 3 2
#4 FALSE NA NA
#5 FALSE NA NA
#6 TRUE NA NA
#7 FALSE NA NA
#8 FALSE 8 6
#9 TRUE NA NA
#10 TRUE NA NA
#11 FALSE NA NA
#12 FALSE 12 10
#13 FALSE NA NA
#14 FALSE NA NA
#15 FALSE NA NA
Upvotes: 3