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
On Sun, Sep 12, 2010 at 9:40 AM, Sebastian Gibb <lists at sebastiangibb.de> wrote:> But I want to do a "sapply" over the "values" vectors. >Try multivariate apply. For more on loops and the apply family check [1]. You might also want to check the plyr package and its documentation. Liviu [1] http://promberger.info/files/rnews-vectorvsloops2008.pdf> mapply(mean, tree[[1]]$node$values, tree[[2]]$node$values)[1] 1 2 3 4 5 6 7 8 9 10> mapply(mean, tree[[1]]$node$values, tree[[2]]$node$values, SIMPLIFY=F)[[1]] [1] 1 [[2]] [1] 2 [[3]] [1] 3 [[4]] [1] 4 [[5]] [1] 5 [[6]] [1] 6 [[7]] [1] 7 [[8]] [1] 8 [[9]] [1] 9 [[10]] [1] 10>