8
In practical terms there seems to be no difference, but underneath the cloth as language processes there is some difference?
8
In practical terms there seems to be no difference, but underneath the cloth as language processes there is some difference?
7
There is no difference in the vast majority of cases. Commands
x <- 5
and
x = 5
are identical.
However, if you want to assign arguments to a function, you are required to use =
. For example, to generate a sample of 10 observations of a normal random variable with mean 5 and standard deviation 2, only the command
rnorm(10, mean = 5, sd = 2)
works. No use trying to rotate
rnorm(10, mean <- 5, sd <- 2)
that you will not get the desired result.
Particularly, I prefer to use <-
whenever I’m going to assign some value to an object. I find it more elegant, because it differentiates from assigning arguments to functions. But it’s all about style anyway.
6
Complementing Marcus' answer. An interesting point is precedence of those operators. The <-
comes before =
. What makes that:
> a <- b = 1
Error in a <- b = 1 : não foi possível encontrar a função "<-<-"
It doesn’t work, but:
> a = b <- 1
> a
[1] 1
> b
[1] 1
Work.
This is the only case I’ve ever seen wrong... But as it is very unusual, it turns out whatever is used.
In fact, I just saw that this example is in pecedência.
## '=' has lower precedence than '<-' ... so you should not mix them ## (and '<-' is considered better style anyway): ## Consequently, this gives a ("non-catchable") error x <- y = 5 #-> Error in (x <- y) = 5 : ....
If you call code style, most books recommend using <-
. Example:
Assignment Use <-, not =, for assignment.
# Good x <- 5 # Bad x = 5
Assignment
Use <-, not =, for assignment.
GOOD:
x <- 5
BAD:
x = 5
In Rstudio, you can use Alt + - to do the <-
.
Browser other questions tagged r operators
You are not signed in. Login or sign up in order to post.
Rotate
rnorm(10, mean <- 5, sd <- 2)
works. Does this not vary between versions of R?– neves