[R] assign values

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sun Nov 13 21:44:51 CET 2005


Robert <samrobertsmith at yahoo.com> writes:

> Professor,
> What does if ((x=2)) 2 mean?
> Thanks,
> Robert

it assigns 2 to x, then uses the result (i.e. whether it is non-zero)
as the condition for the if() construct. So in this case, the
condition is always TRUE and the result is always 2.
 
> Peter Dalgaard <p.dalgaard at biostat.ku.dk> wrote:
> Prof Brian Ripley writes:
> 
> > Please do read the posting guide.
> > The answer to your question can be found by help("<-") or help("=").
> > 
> > The '<-' can be used anywhere, but the '=' is
> > only allowed at the top level (that is, in the complete expression
> > typed by the user) or as one of the subexpressions in a braced
> > list of expressions.
> > 
> > If you don't fully understand that, just use <- always.
> 
> The text is not completely accurate though:
> 
> > if ((x=2)) 2
> [1] 2
> > x
> [1] 2
> > if (x=2) 2
> Error: syntax error in "if(x="
> > if(x<-2) 2
> [1] 2
> > if(x < -2) 2
> >
> 
> We do allow "=" inside parentheses too, it's only in function
> arguments (where it is ambiguous) and inside conditional clauses
> (where users might use it incorrectly to test for equality) that we
> disallow it. 
> 
> As the last example shows, we do leave people to fry in their own fat
> if they use "<-" for comparisons with negative numbers, though (been
> there, done that...)
> 
> 
> -- 
> O__ ---- Peter Dalgaard Øster Farimagsgade 5, Entr.B
> c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
> (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
> ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
> 
> 		
> ---------------------------------
>  Yahoo! FareChase - Search multiple travel sites in one click.  

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list