New course! Every coder should learn Generative AI!
Try a free lesson+ 17
Here's something you might find interesting 👇
https://stackoverflow.com/questions/51547389/why-are-there-two-assignment-operators-and-in-r
+ 6
Not a direct answer, but:
Doing data preparation and analysis in R, I have never used (or needed) "->"
Concerning scripts (not directly typed to console code): I think our instructor used to tell us, he considered "->" bad style or something alike... (it's some time ago, I might be mistaken)
edit:
R also allows using "="
x = 1
but
2 = y # error
edit 2:
x <<- 1
+ 2
I'm only halfway through the R course here but haven't encountered front arrow yet. I guess it's a lot rarer.
0
-> is considered bad practice, but there's also two others.
= Is instanced
<<- is global
So,
> mean(x = 1:5)
> x
Will return
[1] 3
[1] Error: object "x" not found
because x is only defined for that single run of the code
Next,
> mean(x <- 1:5)
> x
Will return
[1] 3
[1] 1 2 3 4 5
Because after the code is ran, it assigns every number from 1 to 5 inside a variable called x
Last,
> foo <- function() {
> bar <- 5
> bar
> }
> foo()
> bar
Will return
[1] 5
[1] Error: object "bar" not found
Because it only assigned bar with a value of 5 inside the function
But
> foo <- function() {
> bar <<- 5
> bar
> }
> foo()
> bar
Will return
[1] 5
[1] 5
Because it assigned the bar variable outside of the function as 5