[R] Comparing dates in two large data frames

Kulupp ku|upp @end|ng |rom on||ne@de
Sat Apr 10 13:06:15 CEST 2021


Dear all,

I have two data frames (df1 and df2) and for each timepoint in df1 I 
want to know: is it whithin any of the timespans in df2? The result 
(e.g. "no" or "yes" or 0 and 1) should be shown in a new column of df1

Here is the code to create the two data frames (the size of the two data 
frames is approx. the same as in my original data frames):

# create data frame df1
ti1 <- seq.POSIXt(from=as.POSIXct("2020/01/01", tz="UTC"), 
to=as.POSIXct("2020/06/01", tz="UTC"), by="10 min")
df1 <- data.frame(Time=ti1)

# create data frame df2 with random timespans, i.e. start and end dates
start <- sort(sample(seq(as.POSIXct("2020/01/01", tz="UTC"), 
as.POSIXct("2020/06/01", tz="UTC"), by="1 mins"), 5000))
end   <- start + 120
df2 <- data.frame(start=start, end=end)

Everything I tried (ifelse combined with sapply or for loops) has been 
very very very slow. Thus, I am looking for a reasonably fast solution.

Thanks a lot for any hint in advance !

Cheers,

Thomas



More information about the R-help mailing list