Skip to content
Prev 174259 / 398506 Next

subtract values

I think the subject heading should be "looping one day at a time". I  
am guessing that one of the zoo functions has already solved that  
problem but it's not a package that I have much (or any) depth. If you  
create a date vector and pass it through as.POSIXct(), you will get a  
series of "midnights".

 >dts <- dates(c("02/27/92", "02/27/92", "01/14/92",
+                "02/28/92", "02/01/92"))
 > dts+2
[1] 02/29/92 02/29/92 01/16/92 03/01/92 02/03/92
 > as.POSIXct(dts)
[1] "1992-02-26 19:00:00 EST" "1992-02-26 19:00:00 EST" "1992-01-13  
19:00:00 EST" "1992-02-27 19:00:00 EST"
[5] "1992-01-31 19:00:00 EST"

Whether that will match what you are calling Bk$DateTime may depend on  
the actual type of those values, which you haven't told us. I do not  
think the call to the for-loop ought to be length of cumPrecip,  
either. Perhaps floor(length(Bk$cumPrecip)/24) or maybe just look at  
the data and figure out what the max of the dates would be.  Perhaps:
 > startdate <- dts[1]
 > ndays < -5
 > for (i in as.POSIXct(startdate +0:ndays) ) {print(as.POSIXct(i,  
origin="1970-01-01"))}
[1] "1992-02-27 EST"
[1] "1992-02-28 EST"
[1] "1992-02-29 EST"
[1] "1992-03-01 EST"
[1] "1992-03-02 EST"
[1] "1992-03-03 EST"