samedi 20 janvier 2018

Set value for the whole day if condtion applies only to two hours

I have a dataframe like this:

start <- as.POSIXct("2015-05-05 06:00:00", format="%Y-%m-%d %H:%M:%S")
end   <- as.POSIXct("2015-05-07 20:00:00", format= "%Y-%m-%d %H:%M:%S")
Time=seq.POSIXt(start, end, by="10 min")

MyData=data.frame(Time=Time,Rain=rep(0,(length(Time))),Rain_Binary=rep(NA,(length(Time))))
MyData[c(2:4,154:157,324:328),2]=5

I'd like to set the value in the Rain_Binary-column to 1 if it rained during that time:

Limit=c(substr(MyData$Time[MyData$Time>= strptime("2015-05-05 06:30:00", "%Y-%m-%d %H:%M:%S") & MyData$Time <= strptime("2015-05-05 09:00:00", "%Y-%m-%d %H:%M:%S")],12,19))

MyData$Rain_Binary=with(MyData, ifelse(((MyData$Rain>0) &  (substr(MyData$Time,12,19) %in% Limit)), 1, 0))

This works as intended. My problem is now that I'd like to set the value to 1 for the WHOLE day if it rained during the time specified in Limit and not only during the specified time.
How do I do that?

Aucun commentaire:

Enregistrer un commentaire