[R] If statement generates two outputs

Berwin A Turlach berwin at maths.uwa.edu.au
Tue Mar 24 02:49:50 CET 2009


G'day Carl,

On Mon, 23 Mar 2009 20:11:19 -0400
Carl Witthoft <carl at witthoft.com> wrote:

>  >From: Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk_at_idi.ntnu.no>
>  >Date: Sun, 22 Mar 2009 22:58:49 +0100
> 
> 
>  >just for fun, you could do this with multiassignment, e.g., using
>  >the (highly experimental and premature!) rvalues:
> 
>  >    source('http://miscell.googlecode.com/svn/rvalues/rvalues.r') 
>  >if (TRUE)
> 
>  >       c(df1, df2) := list(4:8, 9:13)
> 
>  >    dput(df1)
>  >    # 4:8
>  >    dput(df2)
>  >    # 9:13
> 
> 
> -------
> Now THAT's what I call an overloaded operator!   ^_^
> 
> But seriously:  can someone explain to me what's going on in the 
> rvalues.r code?  I tried a simple experiment, replacing ":=" with a 
> "colec" in the code, and of course the line
> 
> c(df1, df2) colec list(4:8, 9:13)
> 
> 
> just gives me a "syntax error" response.   Clearly I need a pointer
> to some documentation about how the colon and equals sign get
> "special treatment somewhere inside R.

Not sure why := gets a special treatment, perhaps because it is not a
valid name and, hence, the parser deduces that it is an operator?

IIRC, the traditional way to define your own operator is to bound the
name by percentage signs, i.e. replacing ":=" by "%colec%" and then
issuing the command

	c(df1, df2) %colec% list(4:8, 9:13)

will work.

Cheers,

	Berwin

=========================== Full address =============================
Berwin A Turlach                            Tel.: +65 6516 4416 (secr)
Dept of Statistics and Applied Probability        +65 6516 6650 (self)
Faculty of Science                          FAX : +65 6872 3919       
National University of Singapore     
6 Science Drive 2, Blk S16, Level 7          e-mail: statba at nus.edu.sg
Singapore 117546                    http://www.stat.nus.edu.sg/~statba




More information about the R-help mailing list