Assignment operators in R: '=' and '<-' -


Assignment operators' = 'and' & lt; - What's the difference between R? I know that operators are slightly different because this example shows

  x   

could not be found but is this the only difference?

When you use them to set a logic value in the function call, the difference is obvious is. For example:

  average (x = 1:10) x ## Error: object 'x' not found   

In this situation, X function, so this user is not present in the workspace.

  middle (x <- 1:10) x ## [1] 1 2 3 4 5 6 7 8 9 10   

this condition In, the user is declared x in the workspace, so that you can use it after the function call is complete.


For assignment '& lt; - To use ', there is a general priority among the community (other than the signature of the function) for older versions of S-Plus (very) for compatibility Note that spaces are clear to such situations Helps like

  x   

To type in most RIDE, type '& lt; To make it easier, the keyboard shortcuts are Ctrl + = , alt + - in the architect. RStudio, Shift + < public To use a more general assignment symbol for the form issued code (for example, on the CRAN), then you should automatically change = to = You can use one of the formatR packages for & lt; - .

  Library (formatR) tidy_source (text = "x = 1: 5", arrow = TRUE) ## x & lt; - 1: 5   

"why x throw an error but x ? "It's under the magic contained in the parser" in the syntax of R, it must be sorted in one way or another by = or Lt; Based on whether or not the - was used, the parser chooses to resolve the bits of expression in different commands.

To understand what is happening, you must know that the assignment silently returns the value that was entrusted. For example print (x <- 2 + 3) .

Second, it is clear that we use prefix signals for assignments. Then

  x   

parser x

  `  

We can expect x then

  `  

but in fact it is

  `=` (`<-`> (X, Y), 5)   

I am not sure why this happens, because perhaps there is a bug in parser, but good fortune Trying to change

= In fact & lt; - is less priority, this is the reason that the order is unexpectedly different.

Help page now shows correctly = .

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -