[R] Loop to find dates whithin dates

William Dunlap wdunlap at tibco.com
Mon Feb 7 19:43:36 CET 2011



Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of patraopedro
> Sent: Monday, February 07, 2011 9:36 AM
> To: r-help at r-project.org
> Subject: Re: [R] Loop to find dates whithin dates
> 
> 
> Thank you for your responses but I think whith some examples 
> the problem will
> be better understated.
> Ok, here it is an example of how the data looks like to allow a better
> understanding of the problem.
> 
> 
> Dframe1 <- data.frame(sunrise = seq(as.POSIXct("2010-07-14
> 06:05:25"),as.POSIXct("2010-07-20 06:05:25"),by="day"),sunset=
> seq(as.POSIXct("2010-07-14 18:00:00"),as.POSIXct("2010-07-20
> 18:00:00"),by="day"))
> 
> Dframe2 <- data.frame(Logtime = seq(as.POSIXct("2010-07-14
> 06:05:25"),as.POSIXct("2010-07-20 06:05:25"),by="hour"),
> temp = runif(145, -5, 15))
> 
> What I'm interested on is rows from the Dframe2 during the 
> day, and for that
> I need (I think) a loop to see if each Logtime occurred 
> during the day or
> night.

That data is very similar to what I showed in my example.
With your data (temp is random):

  > isDuringDaylight <- findInterval(Dframe2$Logtime, with(Dframe1,
rbind(sunrise, sunset))) %% 2 == 1
  > Dframe2[isDuringDaylight,,drop=FALSE]
                  Logtime         temp
  1   2010-07-14 06:05:25  0.360302776
  2   2010-07-14 07:05:25 -2.964403196
  3   2010-07-14 08:05:25 -2.327518053
  4   2010-07-14 09:05:25 10.987051544
  5   2010-07-14 10:05:25 -4.700185475
  6   2010-07-14 11:05:25  9.108118797
  7   2010-07-14 12:05:25  4.750278350
  8   2010-07-14 13:05:25 14.382293951
  9   2010-07-14 14:05:25  2.945564128
  10  2010-07-14 15:05:25  8.433319060
  11  2010-07-14 16:05:25  9.069562554
  12  2010-07-14 17:05:25 -0.437336382
  25  2010-07-15 06:05:25 -3.314539269
  26  2010-07-15 07:05:25 13.032594020
  ...

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> 
> 
> Cheers
> Patrao   
> 
> -- 
> View this message in context: 
> http://r.789695.n4.nabble.com/Loop-to-find-dates-whithin-dates
-tp3264180p3264540.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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