Tuesday, August 6, 2013

Creating a new nested list element that is a combination of two existing nested list elements (in R)

Creating a new nested list element that is a combination of two existing
nested list elements (in R)

I am looking for a hint about how to create a new nested list element from
two existing nested list elements. In the current form of the script I am
working on, I create a list called tardis that is n elements long, based
on the number of elements in an input list. In the example blow, that
input list, dataLayers, list is 2 elements long.
After creating tardis, the script populates it by reading in data from
1200 netCDF files. Each of the 12 elements in 'mean' and 'sd' in tardis
are matrices of geographic data,
tardis[['data']][[decade]][['mean']][[month]], for example, for the 12
calendar months. When the list is fully populated I would like to create
some derived variables. For example, in the snippet below, I would like to
create a variable TOTALPRECIP by adding SNOW and RAIN. In doing this, I
would like to create TOTALPRECIP from SNOW + RAIN as a third list element
in tardis with the exact nested structure as the other two elements
(adding them together and preserving the structure).
Is this possible with apply or its related functions?
begin <- 1901
end <- 1991
dataLayers <- c("SNOW","RAIN")
tardis<-list()
for (i in 1:length(dataLayers)){
tardis[[dataLayers[i]]]<-list('longName'='timeLord','units'='theDr','data'=list())
for (j in seq(begin,end,10)){
tardis[[dataLayers[[i]]]][['data']][[as.character(j<-list('mean'=vector(mode='list',length=12),'sd'=vector(mode='list',length=12))
}
}
#add SNOW AND RAIN
print(names(tardis))
>[1] "SNOW" "RAIN" "TOTALPRECIP"

No comments:

Post a Comment