Variables in R are the same as the notion of variables in any other programming language. Variable is a name we assign for the storage space location that stores our data. A variable in R can be of any datatype, simple or complex. There are certain rules and conventions when working with variables in R.
Variables in R – Naming Rules
The name of a variable is known as an identifier. Not every string can be an identifier in R.
- Variables in R are case sensitive. The names
CARare all treated as different variables in spite of the same spelling.
- Variables can never begin with symbols or numbers.
&carare not valid variable names.
- Variable names can begin with a period (
.). However, if a name starts with a period, it must be followed by a letter instead of a number.
- For defining complex names, period or underscore can be used as the seperator. For example,
a_oneare both valid variable names.
- However, since underscore was used as an assignment operator in earlier versions of R, a period is preferred over underscore.
- Variable names can never contain a blank space.
In the previous post on data types in R, we have introduced the assignment operator in R
<-. This is knows as the gets operator. When we write a statement
a <- b, it can be verbally expressed as, a gets the value of b.
Never put a space between the less than and hyphen in our gets operator. The statement
a <- 10 is an assignment statement, whereas
a < -10 is interpreted as a less than minus 10, resulting in a Boolean output.
In addition to the gets operator, there is also the right assignment operator, that is a reverse of gets
->. The statement
10 -> a is equivalent to
a <- 10.
Also, the equals sign can be used for assignment of variables in R just like C or C++. The statement
a = b is a valid one.
Some Useful Tips with Variables
- When working with complex projects, it sometimes gets tedious to remember all the variable names. Therefore you can type in
ls()in the console to get a list of all active variables.
- To remove a specific variable from the environment the command is
rm(<variablename>). The space gets freed and can be reassigned to another variable now. To remove a variable named x, simply type in
- The code snippet below displays the ls() utility. All the variables and function names in the enivornment get displayed.
> ls()  "a" "a_one" "average" "b" "c" "code"  "division" "elfunction" "fs" "myname" "myname2" "myvector"  "name
- Suppose that we need to remove a variable named average, it can be done by typing in
lsagain, we see that the variable in no longer in the environment.
> rm(average) > ls()  "a" "a_one" "b" "c" "code" "division"  "elfunction" "fs" "myname" "myname2" "myvector" "name"