I have two data frames and wish to use the value in one (DF1$pos
) to search through two columns in DF2 (DF2start, DF2end) and if it falls within those numbers, return DF2$name
DF1
ID pos name
chr 12
chr 542
chr 674
DF2
ID start end annot
chr 1 200 a1
chr 201 432 a2
chr 540 1002 a3
chr 2000 2004 a4
so in this example I would like DF1 to become
ID pos name
chr 12 a1
chr 542 a3
chr 674 a3
I have tried using merge and intersect but do not know how to use an if
statement with a logical expression in them.
The data frames should be coded as follows,
DF1 <- data.frame(ID=c("chr","chr","chr"),
pos=c(12,542,672),
name=c(NA,NA,NA))
DF2 <- data.frame(ID=c("chr","chr","chr","chr"),
start=c(1,201,540,200),
end=c(200,432,1002,2004),
annot=c("a1","a2","a3","a4"))