Olivier Collignon
2001-Oct-30 00:20 UTC
[R] creating chron object aggregates (e.g. sums by day)
What is the recommended/optimal way to perform aggregates on data frames with chron objects? Here is an example:>raw.data1 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 isshort 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
2001-Oct-30 08:35 UTC
[R] creating chron object aggregates (e.g. sums by day)
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Yves Brostaux
2001-Oct-31 09:53 UTC
[R] creating chron object aggregates (e.g. sums by day)
Hello, I have an alternative code to resolve your problem. Not as simple and elegant as I would like, but quite easy to understand and maintain. Here it is : # reading your data, preventing dates to be converted as factor raw.data <- read.table("your data file", as.is=TRUE) # aggregating your observations by day, using a numerical conversion of the dates agg.data.1 <- tapply(raw.data[[2]], as.numeric(dates(raw.data[[1]])), sum) agg.data.1 # moving from the results' table from tapply back to a data frame with named rows agg.data.2 <- data.frame(sumval = agg.data.1[1:length(agg.data.1)]) agg.data.2 # creating the (numerical) date range of your observations date.stamp <- data.frame(date=min(as.numeric(row.names(agg.data.2))):max(as.numeric(row.names(agg.data.2)))) date.stamp # merging the aggregated data and the dates stamps agg.data.3 <- merge(date.stamp, agg.data.2, by.x="date", by.y="row.names", all.x=TRUE) # replacing the NA's where there are no value by zero agg.data.3$sumval[is.na(agg.data.3$sumval)] <- 0 # converting back the numericals to dates agg.data.3$date <- dates(agg.data.3$date) agg.data.3 Hope it helped a bit ! Yves. ==================================================================== YVES BROSTAUX - Ing?nieur agronome Orientation Eaux & For?ts Assistant - Unit? de Statistique et Informatique Gembloux Agricultural University 8, avenue de la Facult? B-5030 Gembloux (Belgium) T?l: +32 (0)81 62 24 69 E-mail : brostaux.y at fsagx.ac.be ==================================================================== At 04:01 31/10/01, you wrote:>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 > > > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Maybe Matching Threads
- [LLVMdev] Passing and returning aggregates (who is responsible for the ABI?)
- sumarizar
- [LLVMdev] Passing and returning aggregates (who is responsible for the ABI?)
- [LLVMdev] Passing and returning aggregates (who is responsible for the ABI?)
- loop avoiding on time interval intersects