[R] subtract values
Kara Przeczek
przeczek at unbc.ca
Thu Mar 19 19:04:23 CET 2009
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
More information about the R-help
mailing list