How To Define Variables And Constants In R

Introduction

 
In this article, I am going to explain about variables and constants in R. I will demonstrate how to define variables, declare variables and how to define constants and use predefined constants in R language.
 

Variables in R

 
In R, we can store data in a memory location and these memory locations have a name: They're called variables. Each and every variable in R can have different name which is referred to as an identifier and these identifiers can store values of different datatypes.
 
There are different ways to define a variable in R which are:
  • In R, a variable always starts with a letter or with a period. A variable if started with a dot cannot be succeeded by a number.
  • Variables cannot be created with keywords which are already predefined in R; that is keywords which are reserved, as their names or identifiers.
  • A variable in R can be defined using just letters or an underscore with letters, dots along with letters. We can even define variables as a mixture of digits, dot, underscore and letters.
 
In R, a few instances of names of variables that are relevant are name, Var, var_1, .var, var.1
In R, a few instances of names of variables which are irrelevant are 5var, var@a, _sub, FALSE, .2ab.
 
From the above example, we can see that in R, to define a variable as a legitimate name of a memory location, we can use underscore at the beginning of the variable name or dot can also be used to separate letters from numbers in variable name. Various examples can be considered such as var.1, var.a, var_1.
 

Assigning values to variables

 
In R, assigning values to a variable can be specified or achieved using the syntax of left angular brackets signifying the syntax of an arrow. After assigning values to a variable, these values can be printed using the predefined cat() function and print() function. To integrate multiple statements assigned to a single variable and separated by double quotation marks and commas, we can use function of cat().
 
Consider the example given below,
  1. > a = c(1,4,3,2)  
  2. > b.6 <- c("Defining functions in","R Language")  
  3. > x_y <- 34  
  4. >   
  5.   
  6. > a  
  7. [11 4 3 2  
  8. >   
  9. > b.6  
  10. [1"Defining functions in" "R Language"                     
  11. >   
  12. > x_y  
  13. [134  
  14. >  
Therefore variable assignment can be done using any of the syntaxes discussed above to assign a variable with a value of different datatypes.
 

Different datatypes of variables

 
Once we declare a variable in R to be of a particular datatype and run the command on R console to get the output of variable, then the same variable can be assigned over and over again with a different value having a different datatype. This shows the dynamic nature of R programming language.
 
Consider the following example,
  1. >   
  2. > a <- ' Greetings'  
  3. > cat("Variable a has a class ",class(a),"\n")  
  4. Variable a has a class character   
  5. >   
  6. > x <- 12  
  7. > cat("Variable x has a class ",class(a),"\n")  
  8.  Variable x has a class numeric  
  9. >   
  10. > name <- 'Sam Hopkins'  
  11. > cat("Variable name has a class ",class(name),"\n")  
  12. Variable name has a class character  

Displaying variables in directory

 
Through the function ls(), the name of all the functions and variables or we can say objects defined within a particular workspace can be obtained in detail. In order to extract a particular from workspace, pattern matching attribute can be used to list the details of the exact variable from a workspace in R.
 
The concept is demonstrated as follows,
  1. > num_var1 <- 40.5  
  2. > num_var2 <- 71  
  3. > obj_datatype <- c("vector""matrix""factor")  
  4.   
  5. > ls()
  6. [1] "num_var1" "num_var2" "obj_datatype"   
structure function in ls
 
in ls.str() function, str() is a separate function which gives more detail about variables rather than just names of the variables.
  1. >   
  2. > ls.str()  
  3. name :  chr "Elijah Hopkins"  
  4. num_var1 :  num 40.5  
  5. num_var2 :  num 71  
  6. obj_datatype :  chr [1:3"vector" "matrix" "factor"  
  7. x :  num 12  
  8. >  
From the code snippet created above, it can be said that all the variables that were defined in a workspace can be listed using the ls() function.
 

Removing directory variables

 
If there are unwanted variables along with functins defined in R console directory, then we can remove and delete definition of such unnecessary variables through rm() function. For example, if we want to remove a variable named “x” defined in R console directory, then we can mention or include the name of that variable inside the function rm() as argument of a function.
 
Following code snippet explains rm() function,
  1. > x<- 12  
  2. > ls.str()  
  3. x :  num 12  
  4. > rm(x)  
  5. > x  
  6. Error: object 'x' not found  
  7. >  
To delete the whole directory in one go, we can use an argument called list inside rm() function as follows rm(list = ls()). The list contains the names of variables in the form of a vector which need to be deleted.
 
Let us take a look at the below code snippet,
  1. >   
  2. > rm(list = ls())  
  3. > ls()  
  4. character(0)  
  5. >  
From the above code snippet, we can see that all the variables inside the directory have been deleted.
 

Defining constants in R

 
In R, we can also generate different types of constants such as numeric and character constants.
 
Numerical type constants
 
Numerical constants comprise all kinds of numbers such as integer and double. We can confirm the datatype of numbers using typeof() function.
 
Here are some examples,
  1. > typeof(5)  
  2. [1"double"  
  3. >   
  4. > typeof(4L)  
  5. [1"integer"  
  6. >   
  7. > typeof(3i)  
  8. [1"complex"  
  9. >  
Character type constants
 
We can also generate constants of datatype character with the help of single and double quotation marks. We can demonstrate that using the below code snippet,
  1. 'student'  
  2. [1"student"  
  3. > typeof("7")  
  4. [1"character"  
  5. >  
Predefined Constants
 
Predefined constants are also available in R. The values of these constants are demonstrated as follows,
  1. print(LETTERS)   
  2.  [1"A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"  
  3. >   
  4.   
  5. >   
  6. print(pi)  
  7. [13.141593  
  8. >   
  9. print(month.name)  
  10.  [1"January"   "February"  "March"     "April"     "May"       "June"      "July"      "August"    "September" "October"   "November"  "December"   
  11. >  

Getting an input from user

 
In R, user data can also be provided as input to R console using the readline() function. The data read by the function can be stored in a variable later. Getting user input can be demonstrated as follows,
  1. > > My_name <- readline(prompt="Enter your name: ")  
  2. Enter first name: abhishek  
  3. >   
  4. > My_age <- readline(prompt="Enter age: ")  
  5. Enter your age: 25  
  6. >   
  7. > My_age <- as.integer(My_age)  
  8. >   
  9. print(paste("Hello,"my name is”, name, "and I am", age+1"years old."))  
  10. [1"Hi, abhishek next year you will be 25 years old.">  

Summary

 
In this article, I explained about variables and constants in R. I also demonstrated how to define variables, declare variables and how to define constants and use predefined constants in R language. Proper coding snippets and output screenshots are also provided.