[R] Loop to find dates whithin dates

Phil Spector spector at stat.berkeley.edu
Thu Feb 10 18:34:44 CET 2011


I can't see a good solution using a loop, but perhaps this
will get you what you want:

> Dframe1$date = as.Date(Dframe1$sunrise)
> Dframe2$date = as.Date(Dframe2$Logtime)
> both = merge(Dframe1,Dframe2)
> both$when = ifelse(both$Logtime >= both$sunrise & 
+                    both$Logtime <= both$sunset,'day','night')
> both$date = both$sunrise = both$sunset = NULL

Then both should be the data frame you want.

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu


On Mon, 7 Feb 2011, patraopedro wrote:

>
> 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.
>
>
> 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.
> ----------------------------------------------------------------------------
> NOTICE: Please note that this eMail, and the contents thereof,
> is subject to the standard Sasol eMail legal notice which may be found at:
> http://www.sasol.com/legalnotices
>
> If you cannot access the legal notice through the URL attached and you wish
> to receive a copy thereof please send an eMail to
> legalnotice at sasol.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