Avi, hi, thanks for your reply. I was vaguely aware of the '...' construct, but hadn't ever worked out how to actually use it. Thank you so much for teaching me that. I agree that I should use it in my function.
Yeah, I just used the parentheses around some of my assignments to avoid the trouble of typing the variable into R to verify the contents. My work is all contained in a Rmarkdown file, that I finally export and file with my notes from class to help me study for exams. Thank you, again, for your generous help for me. -Kevin On Sat, 2025-03-15 at 12:00 +0100, r-help-requ...@r-project.org wrote: > Kevin, > > I was amused by the use of the parentheses wrapping to get the REPL > to show the effects of an assignment but would remove that in any > final program if the output is not needed. > > I am not saying this is wrong, nor what I describe below, but just a > discussion of how others might do it. > > I do somewhat wonder about the way you define the function below: > > compute.diff.means <- function(x) { > return(mean(x[1:n_lt]) - mean(x[(n_lt+1):(n_lt+n_dk)])) > } > > It is a function of x which you pass in but makes use of an external > variable that it needs to find in the environment several times, > n_lt, as well as n_dk. But these assignments happen just once in your > code: > > (n_lt <- length(lt)) > (n_dk <- length(dk)) > > Some people would write the function to include the variables as in: > > compute.diff.means <- function(x, n_lt, n_dk) { > return(mean(x[1:n_lt]) - mean(x[(n_lt+1):(n_lt+n_dk)])) > } > > The names of the variables can be the same or new ones. > > The reason you do this seems to be that you are using "apply" as > shown below and may not know it can accommodate additional argument. > > (random.statistics <- apply(random.samples, 1, compute.diff.means)) > > The above is an implicit loop that calls compute.diff.means() > repeatedly over each row of your matrix. It passes the specific row > as a vector as the first and only argument. > > If you ask "?apply" to document what the apply function does, you may > note that like some other such functions, there is a "..." that > actually means anything else you supply as extra arguments are passed > along to the function. So, since your variables are not changing, > then code like this: > > (random.statistics <- apply(random.samples, 1, compute.diff.means, > n_lt, n_dk)) > > Will call a function with a row vector and then the additional two > arguments so each call will be to: > > compute.diff.means(ROW, n_lt, n_dk) > > Arguably, this approach may be no better but in some sense makes your > function more portable and cleaner. If your code continued and did > additional analyses like this, the function might be more easily re- > usable. > > ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.