[R] Is there an variant of apply() that does not return anything?
There are a few version of apply() (e.g., lapply(), sapply()). I'm wondering if there is one that does not return anything but just silently apply a function to the list argument. For example, the plot function is applied to each element in 'alist'. It is redundant to return anything from apply. apply(alist,function(x){ plot each element of alist}) __ 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] Is there an variant of apply() that does not return anything?
On Nov 20, 2009, at 10:21 AM, Peng Yu wrote: There are a few version of apply() (e.g., lapply(), sapply()). I'm wondering if there is one that does not return anything but just silently apply a function to the list argument. For example, the plot function is applied to each element in 'alist'. It is redundant to return anything from apply. apply(alist,function(x){ plot each element of alist}) Just use a for() loop. If you are plotting things, the performance bottleneck is not going to be in the loop. Sometimes, we get too anal about avoiding for() loops. HTH, Marc Schwartz __ 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] Is there an variant of apply() that does not return anything?
On Thu, Nov 19, 2009 at 2:21 PM, Peng Yu pengyu...@gmail.com wrote: There are a few version of apply() (e.g., lapply(), sapply()). I'm wondering if there is one that does not return anything but just silently apply a function to the list argument. For example, the plot function is applied to each element in 'alist'. It is redundant to return anything from apply. apply(alist,function(x){ plot each element of alist}) Take a look at the l_ply(), a_ply() and d_ply() functions from Hadley's plyr package. They are a refinement and extension to the apply family of functions and the underscore, _, in the function names indicates that they have no return value. -Charlie __ 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] Is there an variant of apply() that does not return anything?
invisible(apply(...)) On Thu, Nov 19, 2009 at 5:21 PM, Peng Yu pengyu...@gmail.com wrote: There are a few version of apply() (e.g., lapply(), sapply()). I'm wondering if there is one that does not return anything but just silently apply a function to the list argument. For example, the plot function is applied to each element in 'alist'. It is redundant to return anything from apply. apply(alist,function(x){ plot each element of alist}) __ 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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? __ 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] Is there an variant of apply() that does not return anything?
On Thu, Nov 19, 2009 at 4:27 PM, Marc Schwartz marc_schwa...@me.com wrote: On Nov 20, 2009, at 10:21 AM, Peng Yu wrote: There are a few version of apply() (e.g., lapply(), sapply()). I'm wondering if there is one that does not return anything but just silently apply a function to the list argument. For example, the plot function is applied to each element in 'alist'. It is redundant to return anything from apply. apply(alist,function(x){ plot each element of alist}) Just use a for() loop. If you are plotting things, the performance bottleneck is not going to be in the loop. Sometimes, we get too anal about avoiding for() loops. Is there a way to get the name of the list in the loop body? List=list(a='c',b='x',e='q') for(x in List) { print(x) } [1] c [1] x [1] q __ 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] Is there an variant of apply() that does not return anything?
I want to use both the name and the content. Although, I could do the following thing. for(x in names(List)) { do some thing with x do some thing with List[[x]] } However, I'd prefer something like the following if R offers such functionality. But it seems not. for(x in List) { do something with the name of x do something with x } On Fri, Nov 20, 2009 at 4:35 PM, Jorge Ivan Velez jorgeivanve...@gmail.com wrote: If I understood correctly for(x in names(List)) print(x) should do what you asked. HTH, Jorge On Thu, Nov 19, 2009 at 5:31 PM, Peng Yu wrote: On Thu, Nov 19, 2009 at 4:27 PM, Marc Schwartz marc_schwa...@me.com wrote: On Nov 20, 2009, at 10:21 AM, Peng Yu wrote: There are a few version of apply() (e.g., lapply(), sapply()). I'm wondering if there is one that does not return anything but just silently apply a function to the list argument. For example, the plot function is applied to each element in 'alist'. It is redundant to return anything from apply. apply(alist,function(x){ plot each element of alist}) Just use a for() loop. If you are plotting things, the performance bottleneck is not going to be in the loop. Sometimes, we get too anal about avoiding for() loops. Is there a way to get the name of the list in the loop body? List=list(a='c',b='x',e='q') for(x in List) { print(x) } [1] c [1] x [1] q __ 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-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] Is there an variant of apply() that does not return anything?
On Thu, Nov 19, 2009 at 5:31 PM, Peng Yu pengyu...@gmail.com wrote: Is there a way to get the name of the list in the loop body? This was just discussed! See this thread: https://stat.ethz.ch/pipermail/r-help/2009-November/218919.html __ 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] Is there an variant of apply() that does not return anything?
On Nov 19, 2009, at 4:31 PM, Peng Yu wrote: On Thu, Nov 19, 2009 at 4:27 PM, Marc Schwartz marc_schwa...@me.com wrote: On Nov 20, 2009, at 10:21 AM, Peng Yu wrote: There are a few version of apply() (e.g., lapply(), sapply()). I'm wondering if there is one that does not return anything but just silently apply a function to the list argument. For example, the plot function is applied to each element in 'alist'. It is redundant to return anything from apply. apply(alist,function(x){ plot each element of alist}) Just use a for() loop. If you are plotting things, the performance bottleneck is not going to be in the loop. Sometimes, we get too anal about avoiding for() loops. Is there a way to get the name of the list in the loop body? List=list(a='c',b='x',e='q') for(x in List) { print(x) } [1] c [1] x [1] q Here is one approach to give you some insight into how to get the name (as opposed to the object itself) of an argument passed to a function: MyList - list(a = 1, b = 2, e = 3) PlotFn - function(x) { # Get the name of the object passed as 'x' (eg. MyList) Main - deparse(substitute(x)) for (i in seq(along = x)) { plot(x[[i]], main = Main, ylab = names(x[i])) } } # Set to pause between each graphic par(ask = TRUE) PlotFn(MyList) Take note that the main title for each graphic will be MyList and the y axis label for each, will be the name of each of the three components in MyList (eg. a, b and e) HTH, Marc Schwartz __ 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.