2017-10-14 210 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

相关问题