Tag Archives: local variables

R variables types

In this post we will deepen the concept of variables in the R environment by providing an explanation of the different types of variables. The R language provides two types of variables:

1) global variables;

2) local variables;

As you can guess, global variables are accessible globally within the program, local variables instead take on meaning only and exclusively in the sector of belonging, resulting only visible within the method in which they are initialized.

For most compilers, a variable name can contain up to thirty-one characters, so that a sufficiently descriptive name can be used for a variable, in R this limit is not indicated. The choice of the name takes on fundamental importance in order to make the code readable; this is because a readable code will be easily maintained even by people other than the programmer who created it.

We have talked about initialization of the variable as an operation to create the variable; let’s see then a trivial example:

> a <- 1

In this instruction the assignment operator (<-) was used, with the meaning of assigning precisely to the memory location identified by the name to the value 1. The type assigned to the variable is established during the initialization phase; it will then be decided whether to assign to it a text string, a Boolean value (true / false), a decimal number etc.