Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are = and <- not equivalent in within()?

Tags:

r

> within( list(a="a",b="b"), c="c" )
Error in eval(expr, envir, enclos) : argument is missing, with no default
> within( list(a="a",b="b"), c<-"c" )
$a
[1] "a"

$b
[1] "b"

$c
[1] "c"

I'm not sure exactly why these two shouldn't be equivalent. It seems like the the = version getting interpreted as an argument named c to within because of the .... Is there any way to disable this behavior? I tried,

within( list(a="a",b="b"), `c`="c" )

but that fails too.

like image 624
Ari B. Friedman Avatar asked May 13 '13 18:05

Ari B. Friedman


People also ask

Is there a difference between <- and in R?

The operators <- and = assign into the environment in which they are evaluated. The operator <- can be used anywhere, whereas the operator = is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions.

Why is <- used in R?

<- is assign operator in R. it can assign a value to a name. x <- 1. 1 is assigned to variable x.

What does <- mean in go?

"=" is assignment,just like other language. <- is a operator only work with channel,it means put or get a message from a channel. channel is an important concept in go,especially in concurrent programming.

Can you use the equal sign in R?

even though the equal sign is also allowed in R to do exactly the same thing when we assign a value to a variable. However, you might feel inconvenient because you need to type two characters to represent one symbol, which is different from many other programming languages.


1 Answers

You are correct that c="c" (or any clause of that form) is getting interpreted as a supplied argument. And no, there's no way to disable that -- it's presumably handled down at the level of the R parser.

This difference between = and <- is documented ?"<-"

The operators ‘<-’ and ‘=’ assign into the environment in which they are evaluated. The operator ‘<-’ can be used anywhere, whereas the operator ‘=’ is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions.

The prime example of a "braced list of expressions" is a function body, which you can verify by typing, e.g. is(body(plot.default)), length(body(plot.default)).

like image 144
Josh O'Brien Avatar answered Oct 12 '22 02:10

Josh O'Brien