[R] Loop to find dates whithin dates

William Dunlap wdunlap at tibco.com
Mon Feb 7 18:52:35 CET 2011


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Eik Vettorazzi
> Sent: Monday, February 07, 2011 9:26 AM
> To: patraopedro
> Cc: r-help at r-project.org
> Subject: Re: [R] Loop to find dates whithin dates
> 
> Hi Patrao,
> you can 'merge' both datasets using the (extracted) day as indicator,
> see ?merge. Then use subset.
> hth.

That approach may work well for this example (outside of the
polar regions), where there is exactly one sunrise and sunset
per day.  When the intervals of interest are more sporadic (say
we have start and stop times for fishing openings) you can
use findInterval(times, rbind(startTimes,endTimes)), as in:

# make fake datasets
nDays <- 1000
nSightings <- 1200
set.seed(1)

dayNumber <- seq_len(nDays) - 1
secondsPerDay <- 60 * 60 * 24
sunTimes <- data.frame(Rise= as.POSIXct("2010-03-21 05:45") + dayNumber*secondsPerDay - 3 * 60 * 60 * sin(dayNumber / 365.25 * 2 * pi),
                       Set = as.POSIXct("2010-03-21 17:45") + dayNumber*secondsPerDay + 3 * 60 * 60 * sin(dayNumber / 365.25 * 2 * pi))

sightings <- data.frame(Time=as.POSIXct("2010-03-21 00:00") + sort(runif(nSightings, 0, nDays * secondsPerDay)),
                      GPS.x = seq_len(nSightings),
                      GPS.y = -seq_len(nSightings))

# map times to daylight or not
i <- findInterval(sightings$Time, rbind(sunTimes$Rise, sunTimes$Set))
isDuringDaylight <- i%%2 == 1 # even intervals at night, odd in daylight

# plot results to see if we are right
with(sunTimes, {
    plot(trunc(Rise, units="days"), rep(0, length(Rise)), type="n", ylim=c(0,24), ylab="Hour of Day", xlab="Date")
    points(pch=".", trunc(Rise, units="days"), as.numeric(Rise-trunc(Rise, units="days"), units="hours"))
    points(pch=".", trunc(Rise, units="days"), as.numeric(Set-trunc(Set, units="days"), units="hours"))
})
with(sightings, points(trunc(Time,"days"), as.numeric(Time - trunc(Time,"days"), units="hours"), col=ifelse(isDuringDaylight,"red","gray")))

Use sightings[isDuringDaylight,,drop=FALSE] to extract the daylight
entries in the sightings data.frame.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 
> 
> 
> 
> Am 07.02.2011 15:10, schrieb patraopedro:
> > 
> > Hello to all,
> > 
> > I have two dataframes, the first with two columns sunrise 
> and sunset (for 10
> > years). Each of these columns is formatted for date time 
> (ex: 01-Jan-2010
> > 15:37:00) 
> > 
> > In the second data frame I have GPS information and also a 
> date time column
> > (same format ).
> > 
> > What I would like to do is a subset of all the rows from the second
> > dataframe that occurred in day time only so between sunrise 
> and sunset.
> > There are thousands of lines from multiple GPS so date time might be
> > repeated in some rows.
> > 
> > Any ideas how to accomplish this? 
> > 
> > Thanks in advance
> > 
> > Patrao
> > 
> 
> 
> -- 
> Eik Vettorazzi
> Institut für Medizinische Biometrie und Epidemiologie
> Universitätsklinikum Hamburg-Eppendorf
> 
> Martinistr. 52
> 20246 Hamburg
> 
> T ++49/40/7410-58243
> F ++49/40/7410-57790
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 



More information about the R-help mailing list