Data types in R

In all programming languages, a data type defines the type of data to be stored in computer memory. A data is a value stored on a variable (link to the variable). So you can define a data type as a classification that represents the type of data like number(8), strings(“hello”), float(8.34), complex numbers(4+3i), etc. Let us learn about the basic data types in the R programming language.

Along with datatype, we do discuss the different types of the data structure being used in R.You may be confused now with data types and data structure terms. A data type is used to assign a single value to a variable and a data structure holds a collection of data or values composed of data types. Let us understand what they really mean in R.

What is a data type in R?

A data type is to denote the type of data used in a program or the type of data a variable will hold. It is an attribute associated with a data to specify its type which further tells the compiler how to interpret the value during program execution.

In R a variable is not declared with any specific datatype rather a variable itself gets the data type of the data assigned to it.

R Data Types

Look at the picture you can notice that in the C program variable a is declared first as an integer data type (int) later it is assigned with a value 1. In the case of R, the initialization of variable 'a' along with the declaration of its data type (integer data type) is stated in a single line. Thus in R programming language, a variable itself gets declared with the data type of value assigned to it.

Note: In R a variable gets the data type of the object (data) assigned to it

What are the basic data types in R?

The following are the five basic data types in the R programming language.

  1. Logical
  2. Integer
  3. Numeric
  4. Complex
  5. Character
R Data Types

Logical Data type in R

In R programming language, logical data type possess (own) two possible values either TRUE or FALSE.

R Data Types

When a value TRUE is assigned to a variable B  by a programmer during the program execution the compiler interprets the value as a logical data type. In other words, the type of data contained by variable B is logical. Here thus the attribute is a logical type associated with B.

Program to show logical data type


# R program to show logical data type

# Assigns  values
A = TRUE
B = FALSE
C  = 1
D  = 8

# Comparing two values  (1>8) using logical operator > returns logical values
E = C > D     

# print the values
print(A)
print(B)
print(C)
print(D)
print(E)

# print the class name of z
print(class(A))
print(class(B))
print(class(E))

# print the type of z
print(typeof(A))
print(typeof(B))
print(typeof(C))
 

Output:


[1] TRUE
[1] FALSE
[1] 1
[1] 8
[1] FALSE
[1] "logical"
[1] "logical"
[1] "logical"
[1] "logical"
[1] "logical"
[1] "logical"

Numeric Data type

In R programming language numeric data type supports both decimal values (4.5, 0.001) and non-decimal (1, 4, 78, and 95678) values.

R Data Types

Variable A is assigned with value 0.9999 of numeric data type.

Program to show numeric data type


# R program to show numeric  data type

# variables A ,B Assigned  with values 1.11 & 8

A  = 1.11
B  = 8


# print the values  of A & B
print(A)
print(B)


# print the class name of A & B
print(class(A))
print(class(B))


# print the type of A & B
print(typeof(A))
print(typeof(B))

 

Output:


[1] TRUE
[1] FALSE
[1] 1
[1] 8
[1] FALSE
[1] "logical"
[1] "logical"
[1] "logical"
[1] "logical"
[1] "logical"
[1] "logical"

Integer Data type

R programming language supports to store of integer values. A value is defined as an integer value by appending “L” at the end of it.

R Data Types

Program to show integer data type


#  R program to show integer data type

# variables A  Assigned  with values 1L

A  = 1L

# print the values
print(A)

# print the class name 
print(class(A))

# print the type 
print(typeof(A))

 

Output:


[1] 1
[1] "integer"
[1] "integer"

Variable A is stored as integer data type with value 1 in memory location.A decimal point value cannot be appended with “L” which results in an warning message as shown in below output.

Program showing error


# A R program to show error in integer data type

# variables A  Assigned  with values 1L
A  = 1L
B = 1.56L

# print the values
print(A)
print(B)
 

Output:


[1] 1
[1] 1.56
Warning message:
In source("C:/Users/Desktop/R/R Pgms/4_Tutorial_numeric type.R") :
integer literal 1.56L contains decimal; using numeric value

The output detect that 1.56 is of numeric data type since it is a decimal value not an integer number.

Note: In R when values are appended with “L” as its suffix it means it is an integer data type.

Complex data type

R supports complex numbers, the numbers with real and imaginary part. The complex data type is used to specify the complex numbers in R.

Program to show complex data type


# A R program to show complex data type
# variables A  Assigned  with values -3+4i

A  = -3+4i
# print the values
print(A)

# print the class name 
print(class(A))

 

Output:


[1] -3+4i
[1] "complex"

Character Data type

R supports to store string values such a sequence of alphabets or a single letter (alphabet), digit, special symbols. Inorder to make compiler understand it is character data type you need to specify them inside single quotes (‘ ’) or double quotes (“ “).

Program to show Character data type


# A R program to show character data type

# variables  Assigned  with  values 

Lang = "Learn eTutorials"
Year = "2022"
Tutorial ='4.0'

# print the values
print(Lang )
print(Year )
print(Tutorial )

# print the class name 
print(class(Lang))
print(class(Year))
print(class(Tutorial))

Output:

[1] "Learn eTutorials"
[1] "2022"
[1] "4.0"
[1] "character"
[1] "character"
[1] "character"

Note: In addition to these basic data type there is one more data type known as raw.

Raw data type

A raw data type holds byte values.

Program to show complex data type


#Raw Data type  
A <- charToRaw("Learn eTutorials")  
cat(A,"\n")  
cat("The data type of A is ",class(A),"\n\n")

Output:

4c 65 61 72 6e 20 65 54 75 74 6f 72 69 61 6c 73 
The data type of A is  raw

Table to summarize the basic data types

Basic Data  Types Description Example
Logical Takes either a TRUE or FALSE value.   TRUE or FALSE
Integer Take whole number values positive integers and 0.   0L, 56L, 7990L
Numeric Takes both whole numbers and decimal point values. 0, 10, 0.009, 5.6
Complex Takes values with real & imaginary part. 1+2i , -3+4i
Character Takes a single character or sequences of words “A”,   “HELLO”
raw raw data type holds byte values.   4c 65 61 72 6e 20 65 54 75 74 6f 72 69 61 6c 73  

Program to show all basic data types in R


# A simple program to show all basic data types in R


# Assign value to variables
A = 0.9999        # Numeric data type

B = TRUE          # logical data type
C= 6L             #integer data type    
D="HELLO"         #character date type
E = 1+2i          #complex data type

# print the class name of variable
cat("The data type of A is ",class(A),"\n")
cat("The data type of B is ",class(B), "\n")
cat("The data type of C is ",class(C),"\n")
cat("The data type of D is ",class(D),"\n")
cat("The data type of E is ",class(E),"\n")


Output:


The data type of A is  numeric 
The data type of B is  logical 
The data type of C is  integer 
The data type of D is  character 
The data type of E is  complex

Some of the R built-in functions class()& typeof() are used in above programs which helps a programmer(user) to identify the features of corresponding variable or data.

How integer data types differ from numeric types in R?

Integers cannot store values with decimal points associated with it. Inorder to store decimal point values in R you can use numeric data type. To specify a value as integer type append “L” at the end of each values.

The below examples use vector data structure, which is consist of one or more elements of same data type. It is represented as c(),inside parentheses contains values with common datatype.

Integer data type Numeric data type
  • Does not support decimal point values
  • Append “L” at end of values to specify as integer data type
  • Supports both decimal point values as well as normal values.
  • If a single value in a set is denoted as floating-point the whole set values get converted to point values. (example 2)
  • If set of values are normal numbers returns as such.(example 3)

Example 1:

vector1 = c(0L, 3666L, 1000000L )

class(vector1)

Output

[1] "integer"

[1] 0 3666 1000000

Example 2:

vector2 = c(5, 45, 1.6890)

class(vector2)

Output

[1] "numeric"

[1] 5.000 45.000 1.689

Example 3 :

vector3 = c(0,3666,1000000)

class(vector3)

Output

[1] "numeric" 0 3666 1000000