Loading, please wait...

A to Z Full Forms and Acronyms

What are the Data Types in the R programming language?

Nov 22, 2021 #RLanguage #Programming, 678 Views
In this article, you will understand the data types in the R programming language.

What are the Data Types in the R programming language? 

DATA TYPES: 

It is a data item used by the programming language to perform the operations. 

The types of data types used in the R programming language are:

  • Vectors
  • Data Frames
  • Array
  • Matrix
  • Lists
  • Factor

R language supports all kinds of primitive data types such as char, integer, double, and complex who can process a single value. 

  • Vector: The complex, character, numeric, integer, and logical are varieties of the vector. Vector is a collection of the same types in a one-dimensional approach. The c() function is used to concatenate all the elements in a single vector. The most used vector functions are length(), is.null(), rep(), class(), is.logical().

Examples of vector

Create a vector with more than one element. For this, we use the c() function to combine all the vector elements into one. 

color < - c('pink', 'green', 'orange')

# print() function is used to print the vector

print(color)

OUTPUT:

[1] "pink" "green" "orange"

# class() function is used to obtain the class of the vector

print(class(color)) 

OUTPUT:

[1] "character"

  • Data Frames: The data items represent in tabular form in data frames data type or can also say it is in a two-dimensional form with an equal length. The data items can be different in columns, but the length of data items should be the same in all the rows. It is a list of vectors of equal length. The data.frame() function is used to create the data frame.

Example of Data Frames:

Y <- data.frame(name = c("John", "Joe", "Sandy"), age=c("32", "19", "25"))

#print data frame

print(Y)

OUTPUT:

1 John 32

2 Joe 19

3 Sandy 25

  • Array: Array is also two-dimensional data set. Unlike matrix, the number of rows and columns are equal. The array() function is used to create an array. 

Example of Array

color <- array(c('pink','blue'),dim = c(3,3,2))

# print array

print(color)

OUTPUT

, , 1

 

     [,1] [,2] [,3]    

[1,] "pink" "blue" "pink" 

[2,] "blue" "pink" "blue"

[3,] "pink" "blue" "pink" 

 

, , 2

 

     [,1] [,2] [,3]    

[1,] "blue" "pink" "blue"

[2,] "pink" "blue" "pink" 

[3,] "blue" "pink" "blue"   

  • Matrices: It is a two-dimensional data set that has rows and columns. Three ways to create a matrix are: 

 -- Using matrix function

 -- Binding vectors

 -- Converting the vector into the matrix

 The Matrix() function is used to create the matrix. 

Example in Matrices

Mat = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)

#print matrices

print(Mat)

OUTPUT

     [,1] [,2] [,3]

[1,] "a" "a" "b" 

[2,] "c" "b" "a"

  • Lists: It contains objects and elements. Elements can be arrays, matrices, characters, or else. It can also contain another list. 

Example of lists

lists_example <- list(c(2,5,3),21.3,sin)

#print lists

print(lists_example)

OUTPUT

[[1]]

[1] 2 5 3

 

[[2]]

[1] 21.3

 

[[3]]

function (x) .Primitive("sin")

  • Factor: factor() function is used to create factor. They are the r-objects. The factor keeps the vector and labels. Labels are characters of any type, such as numeric, character, or boolean. It is useful in statistical modeling.

Example of Factor

colors <- c('green','green','yellow','red','red','red','green')

# Create a factor object.

factor_colors <- factor(colors)

# Print the factor.

print(factor_color)

print(nlevels(factor_color))

OUTPUT

[1] green green yellow red red red green 

Levels: green red yellow

[1] 3

 

 

A to Z Full Forms and Acronyms