Variables in R

Variables in R

  • R
  • 5 mins read

In this article, we will explore what variables are and how they are stored in R.

Variables are the namespace to which a particular object is assigned in order to store its value. Each variable in R is associated with a memory location. Every variable comprises a key-value pair, where the key is the variable name and the value is the data value linked with it. 

Variables can be assigned values belonging to any of the data types, that is, string, integer, or complex numbers. 

Any variable name is composed of letters, numbers, and special characters. However, there is also a limit to the special characters that are allowed. Dot and underlined characters are considered to be valid entries for variable naming. 

A variable may be unidimensional or multi-dimensional in nature and may be assigned to either an array, lists, or multi-dimensional arrays as well. 

Rules for Naming Variables in R

  1. A variable should only be comprised of letters, numbers, and dot or underscore characters. 
  2. Every variable must start with a letter or a dot. Numerical and special characters as first characters are not allowed for the creation of variable names. The dot(.) character is also only allowed if it is followed by a letter and not a numerical value. 
  3. Keywords cannot be used as valid variable names. 
  4. For instance, R is case-sensitive in nature. Therefore, each character holds a different value when specified in upper-case and lower-case characters. For instance, Foxinfotech and foxinfotech are different in R.

Declaring and Assigning Variables in R

Declaring the data type of a variable is not necessary for R. Such languages, which do not require explicit data type declaration, are known as dynamically typed languages, and R is one of these. The interpreter implicitly accesses the data value of the variable and assigns it a type. 

The assignment of the value to any specific variable name can be done in three ways, by either using the equal to (=) operator or the arrow keys, that is, the lefthand (<-) or the righthand (->) arrow. The key difference between both the arrow assignments is that in the lefthand case the assignment takes place from the right to the left place, whereas, it is the opposite in the case of the rightward arrow key. 

#assigning a value 2 to the variable var1 
var1 = 2    

#assigning a string value “FoxInfoTech” to the variable var2   
var3 <- “FoxInfoTech”  

#assigning an integer array value to var3
c(0,1, 2) -> var3 

Printing Variables in R

Variables can be displayed on the console simply by using the print method with the variable name as the parameter inside it. 

print(var-name)

#assigning a value 2 to the variable var1 
var1 = 2    

#assigning a string value “FoxInfoTech” to the variable var2  
var2 <- 'FoxInfoTech'  

#assigning an integer array value to var3
c(0,1, 2) -> var3 

#printing the values of var1, var2 and var3 respectively
print("Variable 1 value")
print(var1)
print("Variable 2 value")
print(var2)
print("Variable 3 value")
print(var3)

The code produces the following output : 

[1] "Variable 1 value"
> print(var1)
[1] 2
> print("Variable 2 value")
[1] "Variable 2 value"
> print(var2)
[1] "FoxInfoTech"
> print("Variable 3 value")
[1] "Variable 3 value"
> print(var3)
[1] 0 1 2

Accessing the data type of the Variable in R

If you wish to explore a particular type of variable in R, the class() method can be used. It indicates whether the declared variable is an integer, character, or string. Accessing the data types is crucial for understanding the compatibility of variables with each other since, in most cases, the variables belonging to the same data types perform operations together. Another important aspect to consider is that all the values, such as an integer, float, double, or an integer array falls under the class of “integer”. Therefore, it only indicates the class to which the data might fall. It doesn’t give any idea about the actual type of data contained within the variable. 

#assigning a value 2 to the variable var1 
var1 = 2    

#assigning a string value “FoxInfoTech” to the variable var2  
var2 <- 'FoxInfoTech'  

#assigning an integer array value to var3
4.5 -> var3 

#printing the values of var1, var2 and var3 respectively
print("Class of Variable 1 ")
class(var1)
print("Class of Variable 2 ")
class(var2)
print("Class of Variable 3 ")
class(var3)

The output produced by the code is : 

[1] "Class of Variable 1 "
> class(var1)
[1] "numeric"
> print("Class of Variable 2 ")
[1] "Class of Variable 2 "
> class(var2)
[1] "character"
> print("Class of Variable 3 ")
[1] "Class of Variable 3 "
> class(var3)
[1] "numeric"

Removing Variables from the workspace

The variables can be deleted from the storage using the rm() command. It removes the instance of that particular variable specified as the parameter in this method. 

#assigning a value 2 to the variable var1 
var1 = 2    
 
#printing the values of var1 respectively
print("Value of Variable 1 ")
print(var1)

#removing the variable
rm(var1)

#printing the values of var1 respectively
print("Value of Variable 1 after removing ")
print(var1)

After removing the variable, there is an exception that the variable is not located in the directory. Thereby the following output is produced by the code : 

[1] "Value of Variable 1 "
> print(var1)
[1] 2
> 
> #removing the variable
> rm(var1)
> 
> #printing the values of var1 respectively
> print("Value of Variable 1 after removing ")
[1] "Value of Variable 1 after removing "
> print(var1)
Error in print(var1) : object 'var1' not found