![]() system.time(result = my.test.function(100)) This example from R Inferno produces two different results. When you mix things up, weird things can happen. My personal advice is to use <- for assignment and = for passing arguments. In the following example, I calculate the mean of 3, 20 and 30 and at the same time assign 3 to x. There’s probably not many use cases where you would want it, but it does work. By putting brackets around an assignment, in places where intuitively it shouldn’t work, one can get around these restrictions. in a for loop).īut this is not necessarily true. According to the documentation, it can only be used at the top-level environment, or when isolated from the surrounding logical structure (e.g. In 2001, to bring assignment in R more in line with other programming languages, assignment using the = symbol was implemented. For example rnorm(n = 10, mean = 2, sd = 1) ![]() ![]() Historically, the = symbol was used to pass arguments to an expression. APL was created on an Execuport, a now antique machine with a keyboard that had an “arrow” symbol, and it could be generated with one keystroke. R (created in the early nineties) is actually a modern implementation of S (created in the mid-70’s), and S is heavily influenced by APL. ![]() So why the arrow? Apparently, this is legacy from APL, a really old programming language. If you are unfamiliar with this, try reading up on scope in programming. First, it checks if the variable already exists in the local environment, and if it doesn’t, it will store the variable in the global environment (.GlobalEnv). The double arrow in the last line is somewhat special. The arrows are respectively the leftwards assignment and the rightwards assignment. By default, the variable gets stored in the environment it is being run in. The assign function is the OG here: it assigns a value to a variable, and it even comes with more parameters that allow you to control which environment to save them in. The first five lines of code do exactly the same. In most - if not all - coding languages I know, assignment is done through the equal sign ‘=’, while in R, I was taught to do it through the backarrow ‘ x # Rightwards assignment When I started coding in R, a couple of years ago, I was stunned by the assignment operator.
0 Comments
Leave a Reply. |