[R] - combining lists
pelt
pelt at knmi.nl
Thu Aug 12 11:43:52 CEST 2010
Hi all,
I have used this library to create a (360 day)calendar for my rainfall
data (which is divided over 9 gridcells):
## CODE##
library(udunits)
utInit()
calendar <- att.get.nc(nc,'time','calendar')
T <- var.get.nc(nc,"time")
times.list <- utCalendar(T,"days since
1961-01-01",style='array',calendar=calendar)
## END CODE##
****
To separate months and years I use:
## CODE##
times.ind <- lapply(as.list(1:12), function(x,months) which(months == x),
months = times.list$month)
times.ind2 <- lapply(as.list(1961:1990), function(x,years) which(years
== x),
years = times.list$year)
## END CODE##
Now I have two lists, the first contains months, for example, for
times.ind[1] I get a list of day numbers which are from January. (so
1:30, 361:390 etc)
The other list does the same for years times.ind2[1] gives the days 1:360
It is now possible for me to create for example monthly averages for
each grid cell
##CODE##
pr.monthmean <- sapply(times.ind, function(x,arr) rowMeans(arr[,,x],dims=2),
arr = pr)
## END CODE##
the same can be done for yearly averages.
However, I would like to create a matrix with monthly averages for each
year separately. I think I need to couple times.ind and times.ind2 in
some way, but I do not know how to do this. Is there anyone who can help
me with this?
Thank you in advance,
Kind regards,
Saskia
More information about the R-help
mailing list