[R] 'with' usage question

Roger D. Peng rpeng at jhsph.edu
Thu Oct 7 19:17:02 CEST 2004


I think what's happening is that the function aov.SS1 will look up 
variables in the environment in which it was *defined*, which in your 
case (I'm guessing) is the global environment/workspace.  Therefore, 
if `speed' and `Subject' are not defined in the global environment, 
they will not be found.

-roger

RenE J.V. Bertin wrote:
> Hello,
> 
> I'm having a little argument with the 'with' function. I have 
> 
> 
> aov.SS1 <- function( y, indep=speed, fSnr=Subject, ... )
> {
> 	indep <- factor(speed)
> 	fSnr <- factor(fSnr)
>   ## ....
> }
> 
> and a dataframe containing speed, Subject, and a bunch of other columns. If I now do
> 
> 
>>with( data, aov.SS1( y=Obs ) )
> 
> 
> I get a message
> 
> Error in factor(indep) : Object "speed" not found
> 
> It seems that automatic argument initialisation doesn't work, as the only effectively valid call seems to be
> 
> 
>>with( data, aov.SS1( y=Obs, indep=speed, fSnr=Subject ) )
> 
> 
> whereas
> 
> 
>>with( data, speed )
> 
> 
> prints what one would expect (i.e. data$speed).
> 
> How come? From ?with, one learns that 
>      "... assignments within 'expr' take place in the constructed
>      environment and not in the user's workspace."
> 
> Which to me reads that a with statement as above is equivalent to
> 
> 
>>attach(data) ; aov.SS1(y=Obs) ; detach(data)
> 
> 
> Or is that just wishful thinking??
> 
> RenE
> 
> ______________________________________________
> 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