Re: [R] apply over parallel lists and their elements
On Tue, Sep 14, 2010 at 12:44 AM, David Winsemius dwinsem...@comcast.net wrote: The second argument to mean is trim. I am not sure what mean(1, 3) is supposed to do but what it return is 1. Thanks for the info. On this particular point I find the documentation confusing. In ?mapply : '‘mapply’ applies ‘FUN’ to the first elements of each ... argument, ' mapply(FUN, ..., MoreArgs = NULL, [..] ' ...: arguments to vectorize over (list or vector).' In my understanding this suggests that '...' can take several comma separated objects, so that in mapply(mean, tree[[1]]$node$values, tree[[2]]$node$values) the second object should not be treated as a 'MoreArgs' argument. But I'm probably wrong. If you wanted 2,3,4 ..., 11 then you would perhaps do: mean( mapply(c, tree[[1]]$node$values, tree[[2]]$node$values) ) I think the original poster was more interested in finding the mean() by rows. Instead of mean( mapply(c, tree[[1]]$node$values, tree[[2]]$node$values) ) [1] 6.5 he probably looks for apply( mapply(c, tree[[1]]$node$values, tree[[2]]$node$values), 2, mean ) [1] 2 3 4 5 6 7 8 9 10 11 although I'm positive there is a neater way to do this. For example, apply( data.frame(tree[[1]]$node$values, tree[[2]]$node$values), 1, mean ) Assembling your data in a data.frame prior to using an *pply function would eliminate the need to write them all by hand. Regards Liviu __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] apply over parallel lists and their elements
Hello On Sun, Sep 12, 2010 at 5:37 PM, Sebastian Gibb li...@sebastiangibb.de wrote: Hello, thanks for your answer. mapply fits to my needs. One thing that seems strange is that if you use tree[[1]]$node$values - 1:10 tree[[2]]$node$values - 3:12 you still get mapply(mean, tree[[1]]$node$values, tree[[2]]$node$values) [1] 1 2 3 4 5 6 7 8 9 10 But I cannot understand what's wrong. But I don't know how many items would tree have. I can't write them all by hand. How can I generate the arguments for mapply? Unfortunately I cannot think of a solution. Perhaps reorganise your data, so that all relevant data points go into the same vector. For example, tree[[4]]-list(); tree[[4]][['node']]-list(); tree[[4]]$node$more[[1]] - 1:10 tree[[4]]$node$more[[2]] - 3:12 mapply(mean, tree[[4]]$node$more) [1] 5.5 7.5 Also check maply and mlply to see if they can fit your needs. [1] Regards Liviu [1] http://had.co.nz/plyr/plyr-intro-090510.pdf mapply(mean, tree[[1]]$node$values, tree[[2]]$node$values, ... tree[[k]]$node$values); Kind regards, Sebastian __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Do you know how to read? http://www.alienetworks.com/srtest.cfm http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader Do you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] apply over parallel lists and their elements
On Sep 13, 2010, at 5:20 PM, Liviu Andronic wrote: Hello On Sun, Sep 12, 2010 at 5:37 PM, Sebastian Gibb li...@sebastiangibb.de wrote: Hello, thanks for your answer. mapply fits to my needs. One thing that seems strange is that if you use tree[[1]]$node$values - 1:10 tree[[2]]$node$values - 3:12 you still get mapply(mean, tree[[1]]$node$values, tree[[2]]$node$values) [1] 1 2 3 4 5 6 7 8 9 10 But I cannot understand what's wrong. The second argument to mean is trim. I am not sure what mean(1, 3) is supposed to do but what it return is 1. If you wanted 2,3,4 ..., 11 then you would perhaps do: mean( mapply(c, tree[[1]]$node$values, tree[[2]]$node$values) ) -- David But I don't know how many items would tree have. I can't write them all by hand. How can I generate the arguments for mapply? Unfortunately I cannot think of a solution. Perhaps reorganise your data, so that all relevant data points go into the same vector. For example, tree[[4]]-list(); tree[[4]][['node']]-list(); tree[[4]]$node$more[[1]] - 1:10 tree[[4]]$node$more[[2]] - 3:12 mapply(mean, tree[[4]]$node$more) [1] 5.5 7.5 Also check maply and mlply to see if they can fit your needs. [1] Regards Liviu [1] http://had.co.nz/plyr/plyr-intro-090510.pdf mapply(mean, tree[[1]]$node$values, tree[[2]]$node$values, ... tree[[k]]$node$values); Kind regards, Sebastian __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Do you know how to read? http://www.alienetworks.com/srtest.cfm http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader Do you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] apply over parallel lists and their elements
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 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] apply over parallel lists and their elements
On Sun, Sep 12, 2010 at 9:40 AM, Sebastian Gibb li...@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 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] apply over parallel lists and their elements
Hello, thanks for your answer. mapply fits to my needs. But I don't know how many items would tree have. I can't write them all by hand. How can I generate the arguments for mapply? mapply(mean, tree[[1]]$node$values, tree[[2]]$node$values, ... tree[[k]]$node$values); Kind regards, Sebastian __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.