I have a data frame with position ranges (Initial and Final) and a Position vector like:
df <- data.frame(
row = c("A", "B", "C", "D"),
Initial = c(10, 26, 48, 69),
Final = c(23, 47, 64, 80))
row Initial Final
1 A 10 23
2 B 26 47
3 C 48 64
4 D 69 80
Vector:
Position <- c(32, 54, 82)
I would like to filter rows that include a position that belongs to the vector. The output would be:
row Initial Final
1 B 26 47
2 C 48 64
Please note the last position did not match to the df Initial-Final position range. Is there a tidyverse solution?
inner_join(df, data.frame(Position), join_by(Initial < Position, Final > Position))
row Initial Final Position
1 B 26 47 32
2 C 48 64 54
subset(df, mapply(\(x,y)any(x < Position & y > Position), df$Initial, df$Final))
row Initial Final
2 B 26 47
3 C 48 64