Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Sciencebreadcumb forward arrow iconTop 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
Read Time
7 Mins
share image icon
In this article
Chevron in toc
View All
Top 5 R Data Types | R Data Types You Should Know About

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”

Read: Variables and Data Types in Python [An Ultimate Guide for Developers]

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

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”  

Our learners also read: Learn Python Online for Free

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  

Also Read: R Developer Salary in India: For Freshers & Experienced

Read our popular Data Science Articles

upGrad’s Exclusive Data Science Webinar for you –

Transformation & Opportunities in Analytics & Insights

Top Data Science Skills to Learn to upskill

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.

Profile

Rohit Sharma

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

Frequently Asked Questions (FAQs)

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

Explore Free Courses

Suggested Blogs

Top 13 Highest Paying Data Science Jobs in India [A Complete Report]
905236
In this article, you will learn about Top 13 Highest Paying Data Science Jobs in India. Take a glimpse below. Data Analyst Data Scientist Machine
Read More

by Rohit Sharma

12 Apr 2024

Most Common PySpark Interview Questions &#038; Answers [For Freshers &#038; Experienced]
20916
Attending a PySpark interview and wondering what are all the questions and discussions you will go through? Before attending a PySpark interview, it’s
Read More

by Rohit Sharma

05 Mar 2024

Data Science for Beginners: A Comprehensive Guide
5067
Data science is an important part of many industries today. Having worked as a data scientist for several years, I have witnessed the massive amounts
Read More

by Harish K

28 Feb 2024

6 Best Data Science Institutes in 2024 (Detailed Guide)
5176
Data science training is one of the most hyped skills in today’s world. Based on my experience as a data scientist, it’s evident that we are in
Read More

by Harish K

28 Feb 2024

Data Science Course Fees: The Roadmap to Your Analytics Career
5075
A data science course syllabus covers several basic and advanced concepts of statistics, data analytics, machine learning, and programming languages.
Read More

by Harish K

28 Feb 2024

Inheritance in Python | Python Inheritance [With Example]
17641
Python is one of the most popular programming languages. Despite a transition full of ups and downs from the Python 2 version to Python 3, the Object-
Read More

by Rohan Vats

27 Feb 2024

Data Mining Architecture: Components, Types &#038; Techniques
10801
Introduction Data mining is the process in which information that was previously unknown, which could be potentially very useful, is extracted from a
Read More

by Rohit Sharma

27 Feb 2024

6 Phases of Data Analytics Lifecycle Every Data Analyst Should Know About
80752
What is a Data Analytics Lifecycle? Data is crucial in today’s digital world. As it gets created, consumed, tested, processed, and reused, data goes
Read More

by Rohit Sharma

19 Feb 2024

Sorting in Data Structure: Categories &#038; Types [With Examples]
139109
The arrangement of data in a preferred order is called sorting in the data structure. By sorting data, it is easier to search through it quickly and e
Read More

by Rohit Sharma

19 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon