[R] right assignment ("->") and functions

Thomas Lumley tlumley at u.washington.edu
Mon Jun 23 06:32:46 CEST 2003


On Mon, 23 Jun 2003, Robin Hankin wrote:

> Hi everyone
>
> check this out [R-1.7.0]:
>
> R> f1 <- function(x){x^2}
> R> f1 -> f2
> R> f2(4)
> [1] 16
> R>
> R> function(x){x^2} -> f3
> function(x){x^2} -> f3
> R> f3(4)
> Error: couldn't find function "f3"
>
> Why does right assignment "->" work in the first but not the second
> case?  Can anyone else reproduce this?
>

It does work.  It just doesn't do what you expect.

Suppose you typed
 {x^2} -> f3
This would assign x^2 to f3.

So
  function(x)
    {x^2} -> f3

is an anonymous function of one argument, which assigns the square of that
argument to the local variable f3.

To get what you wanted you would need

{function(x) x^2}-> f3

	-thomas




More information about the R-help mailing list