[R] creating chron object aggregates (e.g. sums by day)
Joerg Maeder
joerg.maeder at ethz.ch
Tue Oct 30 09:35:33 CET 2001
hallo,
the following code isn't very pretty, but it works. The great problem is
the format of the date.
#read your data
raw.data <- read.table('yourdatafile')
#collect the values of a single day (defined by the first 8 characters
of your date-code)
sum.raw.data <- tapply(raw.data[,2],substring(raw.data[,1],1,8),sum)
#creating the date range of your final variable
dr <- as.character(seq(ISOdate(2001,7,1,0), ISOdate(2001,7,31,0),
"days"))
#Change the format of them to the same of your input data
sdr <- paste(substring(dr,6,7),substr(dr,9,10),substr(dr,3,4),sep='/')
#creating a vector with the correct length
arr.data <- rep(0,length(sdr))
#set the names of the single elements
names(arr.data) <- sdr
#now you can access the elements direct with their name
arr.data[names(sum.raw.data)] <- sum.raw.data
#show the result
arr.data
an other (similary) way is, to change the date format of your data
raw.data <- read.table('test.txt')
sum.raw.data <- tapply(raw.data[,2],substring(raw.data[,1],1,8),sum)
new.date <- strptime(names(sum.raw.data),format="%m/%d/%y") #convert
date format
se <- seq(ISOdate(2001,7,1,0), ISOdate(2001,7,31,0), "days")
arr.data <- rep(0,length(se))
names(arr.data) <- substr(as.character(se),1,10)
arr.data[as.character(new.date)] <- sum.raw.data
it's this simple enough?
gruess
joerg ameder
Olivier Collignon wrote:
>
> What is the recommended/optimal way to perform aggregates on data frames
> with chron objects?
>
> Here is an example:
>
> >raw.data
> 1 07/09/01 4000
> 2 07/09/01 2000
> 3 07/11/01 1000
> 4 07/13/01 800
> 5 07/13/01 700
> 6 07/16/01 600
> 7 07/17/01 500
>
> I'm trying to construct a function that would first aggregate the data
> (second column) by day (grouping by the first column) according to a
> function (here "sum", but could be "max" or other)
>
> >chronaggregate(raw.data, sum, "days") #(used "days" since 07/09/01 is
> short for 07/09/01 00:00:00, but could be 07/09/01 00:12:34)
> 1 07/09/01 6000 << sum of data values for day 07/09/01 from
> raw.data
> 2 07/11/01 1000
> 3 07/13/01 1500 << sum of data values for day 07/13/01 from
> raw.data
> 4 07/16/01 600
> 5 07/17/01 500
>
> and insert 0 values for days without data:
>
> 1 07/09/01 6000
> 2 07/10/01 0 << inserted record
> 3 07/11/01 1000
> 4 07/12/01 0 << inserted record
> 5 07/13/01 1500
> 6 07/14/01 0 << inserted record
> 7 07/15/01 0 << inserted record
> 8 07/16/01 600
> 9 07/17/01 500
>
> Is there a simple way to do this?
>
> Thanks,
>
> --
> -Olivier
>
> --
> Olivier Collignon
> Loudcloud, Inc.
> olivier at loudcloud.com
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
--
Joerg Maeder IACETH INSTITUTE
PhD Student FOR ATMOSPHERIC
Phone: +41 1 633 36 25 AND CLIMATE SCIENCE
Fax: +41 1 633 10 58 ETH ZÜRICH Switzerland
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list