Thursday, January 31, 2019

Why does Google's R style guide recommend




I read the Google style guide for R. For "Assignment", they say:





Use <-, not =, for assignment.



GOOD:
x <- 5



BAD:
x = 5




Can you tell me what the difference is between these two methods of assignment, and why one is to be preferred over the other?



Answer



I believe there are two reasons. One is that <- and = have slightly different meanings depending on context. For example, compare the behavior of the statements:



printx <- function(x) print(x)
printx(x="hello")
printx(x<-"hello")


In the second case, printx(x<-"hello") will also assign to the parent scope, whereas printx(x="hello") will only set the parameter.




The other reason is for historical purposes. Both R, S and the "APL" languages they were based on only allowed arrow key for assignment (which historically was only one character). Ref: http://blog.revolutionanalytics.com/2008/12/use-equals-or-arrow-for-assignment.html


No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...