4 ms·
Per the comments on the article I'd also advise "<-" for assignment rather than "=", reasons (in no particular order): 1) Google says so: http://google-stylegu
by jebus989 13y ago
Per the comments on the article I'd also advise "<-" for assignment rather than "=", reasons (in no particular order):
1) Google says so: http://google-styleguide.googlecode.com/svn/trunk/Rguide.xml http://google-styleguide.googlecode.com/svn/trunk/Rguide.xml
2) It's the accepted convention of the language. When she looks things up of SO, they'll use it; if she looks at a package's source, they'll use it.
3) In RStudio it's "option+hypen" (Mac) to type the four-character " <- ", so no extra typing required.
4) Statistically-speaking, things like x = x + 1 makes no sense
5) You need "<<-" for the occasional global
6) 2 -> x
- anaphor 13y agoAlso, a language shouldn't use the same operator for declaration (initialization in dynamic languages) and assignment imho.
- alecdbrooks 13y agoJust to be clear, R doesn't ever use the same operator for both, it uses == for comparison like C and Python. That being said, extra differentiation is a bonus.
- flyingbrotus 13y ago"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."
- xerula 13y agoAlso "=" is compulsory for setting some named function arguments that have a default value. E.g. format(pi, digits <- 16) only prints with the default number of digits, format(pi, digits=16) works as expected. I think it's nice to have a syntax that distinguishes between object and argument assignment. Also you can do things like this when you're bored: ((a <- b <- 1) + 1 -> c) + 1 -> d (Someone also once told me that "=" for normal assignment sometimes fails in unexpected ways in tryCatch blocks, but I don't know how or why.)