-1

I want to filter the values in df2 in column Change based on the dates between Entry and Exit in df1. For instance between 20030217 and 20030228 there is no reults, therefore the values in Change should be 0. I tried something like this:

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")
user2300940
  • 2,355
  • 1
  • 22
  • 35
  • your question is completely unclear and doesn't provide usable data or examples of the code you have tried. Please see https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and make some corrections. – B Williams Oct 14 '17 at 17:33
  • did you try my approach ? – BENY Oct 14 '17 at 18:00

1 Answers1

2

By using IRanges(Open the link follow the description to install)

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
BENY
  • 317,841
  • 20
  • 164
  • 234