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"
--
David Winsemius
On Mar 19, 2009, at 2:04 PM, Kara Przeczek wrote:
> Dear R-help
> I am using R version 2.6.2. I am trying to subtract specific values
> from a larger data frame. I feel this should be straightforward, but
> I am struggling.
> I have a dataframe "Bk" as follows:
> DateTime cumPrecip
> 01/01/2008 00:00 348
> 01/01/2008 01:00 348
> 01/01/2008 02:00 348
> 01/01/2008 03:00 347
> 01/01/2008 04:00 348
> 01/01/2008 05:00 348
> 01/01/2008 06:00 349
> 01/01/2008 07:00 349
> 01/01/2008 08:00 349
> 01/01/2008 09:00 348
> 01/01/2008 10:00 349
> ... ...
>
> I would like to subtract the cumulative precipitation value at
> 01/01/2008 00:00 from the value at 01/02/2008 00:00 and so on. Thus,
> I would like to subtract cumPrecip at DateTime i from DateTime i+23,
> for each 24 hour period.
> I tried using for loop:
> dPpt=0
> for (i in 1:length(Bk[,"cumPrecip"]))
> {dPpt[i]=diff(Bk[i,(i+23),"cumPrecip"])}
> dPpt=dPpt[seq(1,length(dPpt),24)]
>
> But it had many errors. I also tried using diff(Bk$cumPrecip,
> lag=23) but this moves through the data one step at a time and thus
> calculates a value for every hour of each day, not just midnight.
>
> Thank you very much for your time and assistance!
> Kara
>
>
>
> Kara Przeczek
> M.Sc. Candidate NRES - Environmental Science
> University of Northern B.C.
> 3333 University Way
> Prince George B.C V2N 4Z9
> Phone: (250) 960-5427
> przeczek at unbc.ca
>
>
>
> ______________________________________________
> 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.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT