[R] apply over parallel lists and their elements

Sebastian Gibb lists at sebastiangibb.de
Sun Sep 12 08:40:20 CEST 2010


Hello,

I have a list like the following:

tree<-list(); tree[[1]]$node<-list(); tree[[2]]$node<-list();
tree[[1]]$node$values <- 1:10
tree[[2]]$node$values <- 1:10

After building the list I have to generate the mean of all "values" elements 
with equal indices.
Until now I use something like that:

for(i in seq(along=tree[[1]]$node$values)) {
print(mean(tree[[1]]$node$values[i], tree[[2]]$node$values[i]));
}
[1] 1
[1] 2
...
[1] 10

(I don't need a sapply(tree, function(x)mean(x$node$values));)

But I want to do a "sapply" over the "values" vectors.
Sadly this don't work:
sapply(tree[1:2]$node$values, function(x)mean(x));

Is there another solution?

Kind regards,

Sebastian



More information about the R-help mailing list