Top 5 R Data Types | R Data Types You Should Know About

# Top 5 R Data Types | R Data Types You Should Know About

Last updated:
27th Sep, 2020
Views
7 Mins
View All

Certain variables are needed to store the data that you are using within the program to create any application or render any programming in any programming language. One may also note (usually in typical or most programming languages such as C or C++) that these variables are assigned to specific categories. These categories are what we refer to as the data type.

Data types are a very important concept available in almost all programming languages. As the name indicates, a data type represents a specific kind of data that can be processed using your computer program. Learn about the various data types of Python.

In contrast to other programming languages such as C, the variables are not simply declared as some R data type, but assigned with R objects. The data type of the R object becomes the data type of the variable. There are several types of R objects most common being:

1. Vectors
2. Matrices
3. Lists
4. Arrays
5. Factors
6. Data Frames

Vectors are the most basic R data types holding elements of different classes. There are five major data types of these atomic vectors. The other R-Objects are built upon the atomic vectors.

Learn data science courses from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Mentioned below are some of the R data types used in creating vectors:

## Top R Data Types

### 1. Numeric Data Type

In R programming language, decimal values are called numerics. It is the default R data type assigned to all values in R. Let us understand it with the help of an example:

> y = 11.6 # assigns a decimal value to y variable

> y # prints the value of y

[1] 11.6

> class(y) # prints the class name of y

[1] “numeric”

Here, decimal value (11.6) has been assigned to a variable “y”, whose data type is numeric by default.

Even if you give the variable a non-decimal value, its default data type will still be numeric and not an integer. Here is an example for you:

> y = 1

> y # print the value of y variable

[1] 1

> class(y) # print the class name of y

[1] “numeric”

### 2. Integer Data Type

Numbers without the decimal values are declared under the data type integer in R programming language. To create an integer variable in R, the integer function is revoked. Also, is.integer function can be applied to ensure that y is indeed an integer. Let us look at a few examples to understand the integer data type:

> x = as.integer(5)

> x # print the value of x

[1] 5

> class(x)

[1] “integer”

> is.integer(x) # function to ensure if x is an integer or not

[1] TRUE

Another way to declare an integer is by appending an L suffix.

> x = 5L

> is.integer(x) # function to ensure if x is an integer or not

[1] TRUE

A value with numeric data type can be coerced into an integer data type with as.integer function.

> as.integer(7.16) # coercing a numeric value

[1] 7

A string with decimal values can also be parsed using the as.integer function.

> as.integer(“7.16”) # coercing a decimal string

[1] 7

However, it would be wrong to parse a non-decimal string to the integer function.

Checkout: MATLAB Data Types

### 3. Complex Data Type

A complex value in the R programming language is defined via the pure imaginary value i.

> k = 1 + 2i # creating a complex number

> k

[1] 1+2i

> class(k)

[1] “complex”

### 4. Logical Data Type

The logical data types in R take either true or false value. This value is generated after comparing the two values. Mentioned below is an example for you:

> l = 4; m = 2

> n = l > m # is l larger than y?

> n # printing t the logical value

[1] TRUE

> class(n) # printing the class name of z

[1] “logical”

## Explore our Popular Data Science Online Courses

 Executive Post Graduate Programme in Data Science from IIITB Professional Certificate Program in Data Science for Business Decision Making Master of Science in Data Science from University of Arizona Advanced Certificate Programme in Data Science from IIITB Professional Certificate Program in Data Science and Business Analytics from University of Maryland Data Science Online Courses

### 5. Character Data Type

A character data type is used to represent string values in the R Programming language. Objects are converted into character values using the as.character () function. Mentioned below are some example to built a clear understanding of character data type:

> y = as.character(7.16)

> y # print the character string

[1] “7.16”

> class(y) # print the class name of y

[1] “character”

To concatenate two character values, a paste function can be used.

> fname = “Riya”; lname =”Sharma”

> paste(fname, lname)

[1] “Riya Sharma”

Now, since you have developed an understanding of the most common data types, let us look at how we can create vector objects and use the data types to declare values.

A vector is a set of data elements of the same type of data.

Given below is a vector containing three numeric values 4, 5, and 6.

> c(4, 5, 6)

[1] 4 5 6

And here is a vector of logical values.

> c(FALSE TRUE FALSE TRUE)

[1] FALSE TRUE FALSE TRUE

A vector can also contain character strings.

> c(“AA”, “BB”, “CC”, “DD”, “EE”)

[1] “AA” “BB” “CC” “DD” “EE”

Other R Objects

Matrices

Creates a two-dimensional data set. Here is an example:

Create a matrix.

M = matrix( c(‘q’,’w’,’e’,’r’,’t’,’y’), nrow = 2, ncol = 3, byrow = TRUE)

print(M)

Output –

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

[1,] “q” “w” “e”

[2,] “r” “t” “y”

Lists

A list is a type of object containing different types of elements: vectors, functions, and even other lists.

Example

#Create a list containing vectors and numeric values.

list1 <- list(c(2,5,3),7.9)

# Print the list.

print(list1)

Output –

[[1]]

[1] 2 5 3

[[2]]

[1] 7.9

Arrays

Unlike matrices, arrays can be of n dimensions. The dim attribute used in arrays creates the required number of dimensions. Mentioned below is an example to create an array with two elements with 2X2 matrices.

Create an array.

a <- array(c(‘pink’,’blue’),dim = c(2,2,1))

print(a)

When we execute the above code, it produces the following result −

, , 1

[,1] [,2]

[1,] “pink” “blue”

[2,] “blue” “pink”

Factors

Factors are r-objects created using a vector. Factors store the vectors with distinct values of elements in the vector as labels. The labels always have character data type irrespective of data type in the input vector. Factors are extremely useful in carrying out statistical modelling.

Factors can be created using the factor() function.

# Create a vector.

colors <- c(‘yellow’,’blue’,’pink’)

# Create a factor object.

factor_colors <- factor(colors)

# Print the factor.

print(factor_colors)

[1] yellow blue pink

Data Frames

Data frames are tabular data objects. Each column in a data frame can contain different modes of data. All three columns can have different data types, may it be an integer, numeric, or character.

Data Frames are created using the data.frame() function.

# Create the data frame.

Data <- data.frame(

gender = c(“Male”, “Female”),

height = c(153, 160),

weight = c(80, 78),

Age = c(40 29)

)

print(BMI)

Output

gender height weight Age

1 Male 153.0 80 40

2 Female 160.0 78 29

## Read our popular Data Science Articles

 Data Science Career Path: A Comprehensive Career Guide Data Science Career Growth: The Future of Work is here Why is Data Science Important? 8 Ways Data Science Brings Value to the Business Relevance of Data Science for Managers The Ultimate Data Science Cheat Sheet Every Data Scientists Should Have Top 6 Reasons Why You Should Become a Data Scientist A Day in the Life of Data Scientist: What do they do? Myth Busted: Data Science doesn’t need Coding Business Intelligence vs Data Science: What are the differences?

upGrad’s Exclusive Data Science Webinar for you –

Transformation & Opportunities in Analytics & Insights

## Top Data Science Skills to Learn to upskill

 SL. No Top Data Science Skills to Learn 1 Data Analysis Online Courses Inferential Statistics Online Courses 2 Hypothesis Testing Online Courses Logistic Regression Online Courses 3 Linear Regression Courses Linear Algebra for Analysis Online Courses

## What Next?

If you are curious to learn about R, data science, check out our PG Diploma in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.

#### Rohit Sharma

Blog Author
Rohit Sharma is the Program Director for the UpGrad-IIIT Bangalore, PG Diploma Data Analytics Program.
Get Free Consultation

Select
Select Area of interest
Select Work Experience
By clicking 'Submit' you Agree to

#### Data Science Skills to Master

1What are the drawbacks of using the R programming language?

In many respects, R is insecure. This feature is available in most programming languages, including Python. As a result, R has a variety of drawbacks, one of which being its inability to be used in a web application. R is a challenging language to learn. The learning curve is a bit complex. As a result, learning R may be difficult for individuals who have never coded before. R is connected to the S programming language, which is much older. This means that the basic package doesn't support dynamic or 3D graphics.

2What are R variables?

R variables are usually vectors (lists of data) and can be numeric or text. R variables are usually lower-case, and we use the - operator to assign values to them. Use the c function to make a vector and then list the values.

3

## Suggested Blogs

908669
Wondering what is the range of Python developer salary in India? Before going deep into that, do you know why Python is so popular now? Python has be

by Sriram

21 May 2024

89024
Data structures serve as the backbone of efficient data organization and management within computer systems. They play a pivotal role in computer algo

21 May 2024

22142
Summary: In this Article, you will learn about Data Analyst Salary in India in 2024. Data Science Job roles Average Salary per Annum Data Scient

20 May 2024

134633
Summary: In this Article, you will learn about python free online course with certification. Programming with Python: Introduction for Beginners Le

20 May 2024

248181
In the world of computer science, understanding data structures is essential, especially for beginners. These structures serve as the foundation for

20 May 2024

40861
Summary Pattern in Python or “Python patterns” is an essential part of Python programming, especially when you are just starting out with using algor

19 May 2024

956188
Summary: In this Article, you will learn about 15 exciting data science project ideas & topics for beginners. 1. Beginner Level | Data Science P

16 May 2024

63076
Introduction Sorting is the process of arranging the data in a systematic order so that it can be analysed more effectively. The process of identifyi