1
votes

I have a text file consisting of 6 columns as shown below. the measurements are taken each 30 mint for several years (2001-2013). I want to compute the daily average so for example: for 2001 take all values correspond to the first day (1) and compute the average and do this for all days in that year and also for all years available in the text file.

to read the file:

LR=read.table("C:\\Users\\dat.txt", sep ='', header =TRUE)

header:

head(LR)
   Year day hour mint valu1 valu2
1 2001   1    5   30     0     0
2 2001   1    6    0     1     0
3 2001   1    6   30     2     0
4 2001   1    7    0     0     7
5 2001   1    7   30     5     8
6 2001   1    8    0     0     0
2
You can use dplyr; LR %>% group_by(Year, day) %>% summarise_each(funs(mean), starts_with('valu')) or aggregate(.~Year+day, LR[-c(3:4)], mean)akrun

2 Answers

4
votes

Try:

library(plyr)
ddply(LR, .(Year, day), summarize, val = mean(valu1)) 

And another less elegant option:

LR$n <- paste(LR$Year, LR$day, sep="-")
tapply(LR$valu1, LR$n, FUN=mean)

If you want to select a certain range of years use subset:

dat < ddply(LR, .(Year, day), summarize, val = mean(valu1)) 
subset(dat, Year > 2003 & Year < 2005)
2
votes

You can try aggregate:

res <- aggregate(LR, by = list(paste0(dat$Year, dat$day)), FUN = mean)
## You can remove the extra columns if you want
res[, -c(1,4,5)]

Or as Michael Lawrence suggests, using the formula interface:

aggregate(cbind(valu1, valu2) ~ Year + day, LR, mean)