2017-10-14 209 views
-1

我想根據df1中EntryExit之間的日期過濾df2Change中的值。對於20030217和20030228之間的實例沒有reults,因此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

您的問題完全不清楚,並且不提供可用的數據或您嘗試過的代碼示例。請參閱https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example並進行一些更正。 –

+0

你試過我的方法嗎? – Wen

回答

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 
+0

如何將問題中的數據框複製到R中? – DataTx

+0

@DataTx'df2 < - read.table(text ='copy')' – Wen

+0

Error to(x,...):object'olaps'not found – user2300940

相關問題