我想根据df1中df2
和Change
之间的日期过滤Entry
列Exit
中的值。例如,在20030217和20030228之间没有结果,因此Change
中的值应为0。
我试过这样的事情:
DF1
structure(list(Entry = c(20030127L, 20030128L, 20030129L, 20030205L,
20030210L, 20030228L, 20030307L, 20030310L, 20030313L, 20030331L
), Exit = c(20030128L, 20030129L, 20030205L, 20030210L, 20030217L,
20030307L, 20030310L, 20030311L, 20030320L, 20030401L), Result = c(-132,
-204, -455, -1640, 3678, -1516, -610, -247, 4280, -378)), .Names = c("Entry",
"Exit", "Result"), row.names = c(NA, 10L), class = "data.frame")
DF2
structure(list(V1 = c(20030127L, 20030128L, 20030129L, 20030130L,
20030131L, 20030203L, 20030204L, 20030205L, 20030206L, 20030207L
), V6 = c(475.65, 469.16, 466.82, 479.68, 477.8, 481.8, 464,
476.34, 474.25, 466.97), Change = c(47565, 46916, 46682, 47968,
47780, 48180, 46400, 47634, 47425, 46697)), .Names = c("V1",
"V6", "Change"), row.names = 52:61, class = "data.frame")
答案 0 :(得分:2)
使用IRanges
(打开链接,按照说明进行安装)
library(IRanges)
idx2 <- with(df2, IRanges(V1, width=1, names=df2))
idx1 <- with(df1, IRanges(Entry, Exit, names=df1))
idx <- findOverlaps(idx1, idx2)
df2[-unique(subjectHits(idx)),c('Change')]=0
df2
V1 V6 Change
52 20030127 475.65 47565
53 20030128 469.16 46916
54 20030129 466.82 46682
55 20030130 479.68 47968
56 20030131 477.80 47780
57 20030203 481.80 48180
58 20030204 464.00 46400
59 20030205 476.34 47634
60 20030206 474.25 47425
61 20030207 466.97 46697
62 20030210 456.53 45653
63 20030211 469.07 46907
64 20030212 473.17 47317
65 20030213 474.30 47430
66 20030214 479.38 47938
67 20030217 493.91 49391
68 20030218 499.17 0
69 20030219 491.29 0
70 20030220 479.98 0
71 20030221 478.19 0