[R] Feeding a sequence to a function

Uwe Ligges ligges at statistik.uni-dortmund.de
Sat Apr 22 13:00:42 CEST 2006


Frank Black wrote:

> Dear all,
> 
> I have written a function that takes two arguments, and I would like to feed 
> it all pairs of values from -10 to 10.  The following code works for all 
> pairs of values between 1 and 10, but given R's indexing, I can't extend it 
> back to cover the zeros and the negative values.  I'd appreciate any 
> suggestions for a work-around.
> 
> Thanks!  Fred
> 
> Tau <- matrix(0,10,10)
> S2 <- matrix(0,10,10)
> R2w <- matrix(0,10,10)
> 
> for (i in 1:10) {
>      for (j in 1:10) {
>           out <- function(i,j)
>           Tau[i,j] <- out at Tau
>           S2[i,j] <- out at Sigma2
>           R2w[i,j] <-out at R2w
>      }
> }


Simply shift the stuff as follows:


Tau <- S2 <- R2w <- matrix(0,21,21)

for (i in 1:21) {
      for (j in 1:21) {
           out <- function(i-11, j-11)
           Tau[i,j] <- out at Tau
           S2[i,j] <- out at Sigma2
           R2w[i,j] <-out at R2w
      }
}

Uwe Ligges


> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list