A table with many rows, but for simplicity of the problem...
> df <- data.frame(V1=c("imp: abc","apple","","imp: xyz","","lemon","ball","bell"),
V2=c("1","2","3","4","5","6","7","8"),
V3=c("NA","7","NA","2,8","NA","NA","NA","NA"))
V1 V2 V3
1 imp: abc 1 NA
2 apple 2 7
3 3 NA
4 imp: xyz 4 2,8
5 5 NA
6 lemon 6 NA
7 ball 7 NA
8 bell 8 NA
So what I am trying to achieve is search for the nearest string "imp:" in V1 based on V3 and V2. For example, V3 is 7, then go to row 7 and from there search for the nearest "imp:" in V1 which is nothing but imp: xyz present in 4 th row.
If there are multiple numbers in V3, for example V3 is 2, 8 then first go to row 2 and from there search for the nearest "imp:" in V1 which is nothing but imp: abc present in 1st row and again go to row 8 and from there search for the nearest "imp:" in V1 which is nothing but imp: xyz present in 4 th row.
Here's what output looks like:
V1 V2 V3 V4
1 imp: abc 1 NA
2 apple 2 7 imp: xyz
3 3 NA
4 imp: xyz 4 2,8 imp: abc, imp: xyz
5 5 NA
6 lemon 6 NA
7 ball 7 NA
8 bell 8 NA
NOTE: Search has to be upwards.