簡體   English   中英

根據另一個數據框中的日期過濾數據框中的值

[英]Filter values in data frame based on dates in another data frame

我想根據df1中EntryExit之間的日期來過濾Change列中df2中的值。 例如,在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")

通過使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM