Variables in R
Unlike Java or C#, R enables total flexibility in the assignment of variables. This means that you can assign objects of different types to the same variable. This will cause overriding:
var1 <- 10 var1 <- "a string"
In this case, for instance, R will not throw an error for var1
. In addition, there is no need to pre-declare the class of the variable.
The assignment of variables in R can be done in the following three ways:
<- or ->
: These arrows assign the corresponding value to a variable. However, the first alternative is more common:var1 <- 10 10 -> var1
=
: This is similar to<- or ->
.Tip
As in most programming languages, it is important to keep in mind that whenever a comparison is needed,
==
must be used.assign()
: This is a function with the name of the variable as its first parameter and the value as its second:> assign("var1",6)
For conventional variable assignments, there is no need to use this function. However, it becomes...