4 ms·
In R, it is actually distinguished that way, example: a <- a + 1 a + 1 -> a # also works, but REALLY bad practice But, so does a = a + 1 Granted, there are
by chasedehan 9y ago
In R, it is actually distinguished that way, example:
a <- a + 1
a + 1 -> a # also works, but REALLY bad practice
But, so does
a = a + 1
Granted, there are a bunch of R haters (especially from people with formal CS educations), I think this convention makes a lot of sense. While most will disagree about the '<-' I like it from a code reading sense in that you know that it is an assignment right away. Coming from a mathematical perspective before learning to code, this makes a lot more sense in the 'assignment' fashion.
In case you are wondering, the difference between <- and = in R is in scoping. For example, in the following function call:
foo(x = 'value')
x is declared in the scope of the function, whereas:
foo(x <- 'value')
x is now declared in the user environment. Granted, that is not good practice, but that is why there is a difference.
- thalesmello 9y ago`->` is a bad practice? I like to use it at the end of a long `%>%` pipe. Reading the whole thing feels a lot more natural.
- lionel- 9y agoIt is bad practice because you are hiding the side effect.
- chasedehan 9y agoThe first time I ever saw that you could do that was in a blog post about stringing at the end of the a series of pipes. I thought it was pretty neat (and actually used it a couple times), then had problems when I couldn't figure out why my code was messing up. for example, this assigns a ggplot to 'plot': df %>% na.omit() %>% ggplot(aes(x=x, y=y)) + geom_line() -> plot That is really confusing in that the way most people would read it is that it is something to be plotted. However, the assignment does occur and is masked. having 'plot <- df %>%' as the first line makes it clear that a new object is being created. We actually had to modify our style guide to prevent the '->'
- Myrmornis 9y ago> Granted, there are a bunch of R haters (especially from people with formal CS educations), I think this convention makes a lot of sense. To be clear, the assignment syntax is great, it's other things that are the target of R haters' hate! Like a high-level language without hash tables.
- lionel- 9y agoYou can use environments for hash tables.