[R] tapply

Gabor Grothendieck ggrothendieck at myway.com
Thu Mar 18 15:11:56 CET 2004




Try this (untested):

aggregate( data[,6:8], list(date = as.matrix(data[,1:3]) %*% c(10000,100,1)), mean )

---
Date:   Thu, 18 Mar 2004 09:39:02 +0100 
From:   <mike.campana at freesurf.ch>
To:   <R-help at stat.math.ethz.ch> 
Subject:   [R] tapply 

 
Dear all
I have a dataframe containing hourly data of 3 parameters. 
I would like to create a dataframe containg daily mean values of these 
parameters. Additionally I want to keep information about time of 
measurement ("year","month","day"). 
With the function tapply I can average over a column of the dataframe. 
I can repeat the function 2 time and merge the vectors. In this way I 
obtain my new dataframe (see below).If I want to add the column day, 
month and year I can repeat tapply other three time. This system works. 


Question: is there a function that average in a single step over the 3 
columns?

Thanks a lot for your answer!
Regards
Mike Campana 

#### read the data
setwd("c:/R")
data <- NULL
data <- as.data.frame(read.table(file="Montreal.txt",header=F,skip=15))
colnames(data) 
<-c("year","month","day","hour","min","temp","press","ozone")
### create mean value
temp_daily <- 
tapply(data$temp,data$year*10000+data$month*100+data$day,FUN=mean)
press_daily <- 
tapply(data$press,data$year*10000+data$month*100+data$day,FUN=mean)
ozone_daily <- 
tapply(data$ozone,data$year*10000+data$month*100+data$day,FUN=mean)
### merge the data
newdata <- as.data.frame (cbind(temp_daily,temp_daily,temp_daily))




More information about the R-help mailing list