[R] Re: [S] [R/S] strange solution

Spencer Graves spencer.graves at pdf.com
Wed Sep 1 22:39:25 CEST 2004


Hi, Erin: 

      A cleaner way is to pass "n2" to "outer" as a "..." argument, as 
in the following modification of your code: 

boot1 <- function(y,method="f",p=1) {
n1 <- length(y)
n2 <- n1*p
n3 <- n2 - 1
a <- 0.5*(outer(1:n3,1:n3,function(x,y, n2.){n2. - pmax(x,y)}, n2.=n2))
return(a)
}

y1 <- c( 9,  8, 7, 3, 6)

boot1(y=y1,p=4)

      The use of "assign" like this might be called "dirty programming", 
in the sense that any object "n2" in working directory will be 
overwritten without warning by the function "boot1", and then "boot1" 
leaves the "n2" garbage after it's done.  If you aren't aware of this 
side effect, you could get unpredictable problems with any other use of 
"n2" in the working directory. 

      The difference arises because of differences in the way R and 
S-Plus "scope" variable names.  This is described in the "R Language 
Definition" manual;  search for "scope" and "scoping" or do an R Site 
Search for terms like this at "www.r-project.org". 

      Passing the "n2" as an argument as I did in the above code seemed 
to work for me just now in S-Plus 6.2 and R 1.9.1 under Windows 2000. 

      Hope this helps. 
      Spencer Graves

Erin Hodgess wrote:

>Dear R and S People:
>
>I ended up using the "assign" command, and things work in S+.
>
>boot1 <- function(y,method="f",p=1) {
>n1 <- length(y)
>#n2 <- n1*p
>assign("n2",n1*p)
>n3 <- n2 - 1
>a <- 0.5*(outer(1:n3,1:n3,function(x,y){n2 - pmax(x,y)}))
>return(a)
>}
>
>thanks for listening!
>
>
>Sincerely,
>Erin H
>mailto: hodgess at gator.uhd.edu
>
>--------------------------------------------------------------------
>This message was distributed by s-news at lists.biostat.wustl.edu.  To
>...(s-news.. clipped)...

>  
>

-- 
Spencer Graves, PhD, Senior Development Engineer
O:  (408)938-4420;  mobile:  (408)655-4567




More information about the R-help mailing list