[R] [Q] a dummy variable used with sapply

Erik Iverson eriki at ccbr.umn.edu
Sat Aug 7 21:54:11 CEST 2010


On 08/07/2010 01:00 PM, GMail (KU) wrote:
> Hello,
>
> While learning how to manipulate data with R, I found one example that I
> could not understand.
>
> In the following example, the function definition of "maxcor" has an argument
> named "i" and I don't understand why. Could someone explain why the maxcor
> function definition needs to have this argument?
>
> maxcor = function(i, n = 10, m = 5) { mat = matrix(rnorm(n*m),n,m) corr =
> cor(mat) diag(corr) = NA max(corr,na.rm=TRUE) }
>
>> maxcors = sapply(1:1000, maxcor, n = 100)

Well, the short answer is that you're instinct is correct, it doesn't need it. 
The `i` parameter is just being used as a counter.  `sapply` is passing the 
numbers from 1 to 1000 to this function each time it is invoked, but the 
function definition does not actually use this number.

I'm guessing it is there because whoever wrote maxcor did not know about, or 
understand, the ?replicate function, and just used the sapply function. (Note 
`replicate` is essentially automatically doing what the above code does.)

The following is untested, but will probably work...

maxcor <- function(n = 10, m = 5)
{
     mat <- matrix(rnorm(n*m), n, m)
     corr <- cor(mat)
     diag(corr) <- NA
     max(corr, na.rm = TRUE)
}

maxcors <- replicate(n = 1000, maxcor(n = 100))

where the first n represents the number of replicates to perform,
and the second n is the value passed to maxcor. It's just a coincidence that 
they are named the same thing.



More information about the R-help mailing list