Variables And Data Types In R

In my previous article, I described what R and RStudio are along with the steps involved in installing them. If you are completely new to R, then please read the "Introduction to R and RStudio" article. Also, try out writing your first program in R as described in that article. This article is about the various data types and variables in R. Even if you are not from a programming background, this article will make you understand what data types and variables are and how to use them.
 

What is a variable?

 
A variable in programming is used to store some data which will be used by the program. Consider it as a container which holds the data.
 
For example, we need to add the bill amount with tax payable and generate a total amount. In this case, the value of the bill amount will change for every customer and on the basis of the bill amount, the tax will also change. Thus, the values are varying and we need a container to store them. This container is called a variable.
 
Let us see a small demo for this example.
 
The bill amount for the first customer is Rs. 1000 and the tax which he needs to pay is Rs. 200. The below code snippet describes how a simple addition takes place in R.  
  1. #Customer 1  
  2. billAmt <- 1000  
  3. tax <- 200  
  4. totalAmt <- billAmt + tax  
  5. totalAmt  
In line 2, billAmt <- 1000 lets the computer know that 1000 is to be stored. Where should it store it? In a container labeled as billAmt. Such a container is known as a variable.
 
Similarly, tax <- 200 stores 200 in a variable named tax.
 
Finally, we add the value of billAmt and tax and stored it in totalAmt.
 
In line 5, we wrote totalAmt to print the value of the total on the screen.
 
Copy-paste the above code and try running it. You should get the following output.
 
Variables And Data Types In R
 
As you can see in the screenshot above, we are getting 1200 as the totalAmt.
 
Now, try changing the value of the billAmt and/or tax. The value of totalAmt will also change. As the containers billAmt, tax, and totalAmt are carrying values which change, they are called variables in programming.
 

Few rules to define variables in R

  1. Variable names cannot contain spaces 
    Example - "Bill Amt" is invalid

  2. A variable name can start with a dot but dot should not follow the number. If starting dot is not followed by a number, then it's valid
    Example -.1BillAmt is invalid

  3. A variable name should not start with a number
    Example - 7Name is invalid

  4. A variable name can contain letters, numbers, underscores and dots
    Example - Bill_Name1. is valid
I hope this simple example made you understand what variables are. Now, let us understand various data types in R.
 

Data Types in R

 
Data is available in various forms. In programming, data types are associated with a variable. A data type describes the type of data a variable can hold. Also, it is important to remember that everything in R is an object. 
 
The basic data types in R are as follows,
  1. Character
  2. Numeric
  3. Integer
  4. Logical
  5. Complex  
There are several other data types in R. But, let us understand the above 5, to begin with.
 

Character data type

 
Any group of characters or numbers from your keyboard constitutes the character data type. This data type is also known as the string data type. The string (text) is enclosed in single quotes '...' or in double quotes "..."
 
Try running the following code by replacing Shivaji with your name. Ensure that your name is in single quotes.
  1. name<-'Shivaji'  
  2. name  
In the above code snippet, the object (variable) name is of the character data type.
 
How to check the data type?
 
It's simple. Use the class function as shown below
  1. name<-'Shivaji'  
  2. name  
  3. class(name)  
Line 3, class(name) returns the data type of the variable which is "character".
 
Variables And Data Types In R
 

Numeric data type

 
Any number with or without decimal point, i.e., any decimal value constitute numeric data type. In the example of totalAmt above, the variables billAmt, tax and totalAmt are of numeric data type as the value assigned to them is a number.
  1. billAmt <- 1000    
  2. tax <- 200    
  3. totalAmt <- billAmt + tax    
  4. totalAmt    
  5. class(billAmt)  
  6. class(tax)  
  7. class(totalAmt)  
This will generate the following output,
 
Variables And Data Types In R
 

Integer data type

 
Integer stores non-decimal values. The as.integer() function can be used to convert a number into integer type data in R.
 
Run the following code snippet.
  1. n=5.5  
  2. n  
  3. class(n)  
  4.   
  5. x=as.integer(n)  
  6. class(x)  
  7. x  
When you run the above code, you would observe that class(n) gives you "numeric" but class(x) will give you "integer" as we are using as.integer() function in line number 5 and assigning integer value to variable x.
 
Also, if you observe the values of n and x. n is 5.5 while x just stores the value as 5 and eliminate the decimal part. 
 
Variables And Data Types In R
 

Logical data type

 
This data type stores the value TRUE or FALSE. The values are often generated as a result of logical operations.  
 
Run the following code snippet to understand logical data type better.
  1. x=1  
  2. y=2  
  3. z=x>y  
  4. z  
  5. class(z)  
In the above code snippet, numeric value 1 is assigned to variable x and numeric value 2 is assigned to variable y. This means y is greater than x. Now, in order to check this using R, in line 3, the logical operation "x>y" is performed and the resulting value which is of logical data type is stored in z. Since x is not greater than y, the result is FALSE.
 
Variables And Data Types In R 
 
Try changing values of x and y and observe the output.
 

Complex data type

 
Complex data type consists of complex numbers with real and imaginary parts. 
  1. a<-3+2i  
  2. b<-3-9i  
  3. a  
  4. class(a)  
  5. b  
  6. class(b)  
Desired output looks like as shown below
 
Variables And Data Types In R
 
Besides the basic data types, R has various data structures like Vectors, Arrays, Matrices, etc. 
 
Try all the code samples mentioned in this article and write in the comment section if you discover something new and interesting.
 
Happy Learning! 


Similar Articles