EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials R Programming Tutorial R Data Types
Secondary Sidebar
R programming Tutorial
  • Basic
    • What is R Programming Language
    • Careers in R Programming
    • Install R
    • List of R Packages
    • Introduction of R Tools Technology
    • R Programming Language
    • DataSet in R
    • What is RStudio?
    • R-studio-Functions
    • R Packages
    • Time series?in R
    • R Data Types
    • R for data science
    • R Operators
    • R Data Frame
    • R Analytics Tool
    • R Tree Package
    • Vectors in R
  • Control statement
    • If Statement in R
    • If Else Statement in R
    • Else if in R
    • Switch Statement in R
  • Loops
    • Loops in R
    • For Loop in R
    • Nested For Loop in R
    • While Loop in R
    • Next in R
  • Chart/graphs
    • Graphs in R
    • Bar Charts in R
    • Pie Chart in R
    • Histogram in R
    • Line Graph in R
    • Plot Function in R
    • Scatterplot in R
    • R Boxplot labels
  • Regression in R
    • Simple Linear Regression in R
    • Linear Regression in R
    • Multiple Linear Regression in R
    • Logistic Regression in R
    • Poisson Regression in R
    • OLS Regression in R
    • P-Value in Regression
  • Anova in R
    • ANOVA in R
    • One Way ANOVA in R
    • Two Way ANOVA in R
  • Data Structure
    • R list
    • Arrays in R
    • Data Frames in R
    • Factors in R
    • R Vectors
  • Advanced
    • Statistical Analysis with R
    • R String Functions
    • Data Exploration in R
    • R CSV Files
    • KNN Algorithm in R
    • Sorting in R
    • lm Function in R
    • Hierarchical Clustering in R
    • R Normal Distribution
    • Binomial Distribution in R
    • Decision Tree in R
    • GLM in R
    • Arima Model in R
    • Linear Model in R
    • Predict Function in R
    • Survival Analysis in R
    • Standard Deviation in R
    • Statistical Analysis in R
    • Predictive Analysis?in R
    • T-test in R
    • Database in R
  • Programs
    • Functions in R
    • Boxplot in R
    • R Program Functions
    • Factorial in R
    • Random Number Generator in R
  • Interview question
    • R Interview Questions

Related Courses

R Programming Certification Course

Statistical Analysis Course Training

All in One Data Science Courses

R Data Types

By Priya PedamkarPriya Pedamkar

r data types

Introduction to R Data Types

R data types are the basic features of R language that are used to accept and store various types of data. R language supports commonly used primitive or Scalar data types such as char, integer, double, logical, and complex data types that accept and process as single data value at a time. Also, R language supports vectors that can hold similar data types of one-dimensional. The matrix data types, list, data frames, arrays are for two or multidimensional data formats. There are built-in keywords and functions available in the R language to declare each data type and assign values to the variable related to the data types.

Explain R Data Types

R programming supports various datatypes like scalars, matrices, lists, vectors,s, and data frames. Everything in R is considered an object, which means it stores and processes operations on objects). The key feature of R is a different process is done with different types of objects. Most of the commands in R involves applying functions to the objects. Variables do not require a declaration, instead of assigning a sequence of numbers to the vectors can be done.

Let’s learn the types one by one:

1. Vector

Vector has a set of values with the same types (collection of ordered elements) represented in one dimensional. The class of the vector is determined by the type of entries made. When a vector is created for more than one element, the c () function is used to concatenate all the elements together in a single vector. Vectors are a string of numeric, sequential numbers or random numbers. Vector varieties are the character, integer, numeric, complex, logical (true, false). These are implicit conversions. Some of the functions fives vector functions are  length(), class(x), is.logical(x) , is.null , rep().

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Example

In the below, we can see basic vector examples:

  • Vector Arithmetic: Numeric vectors are performed in arithmetic expressions to do calculations to give another vector. Statistical operations also are done, which gives entries like max, min, var mean.

Code:

y <-c (1, 2 ,2.5 ,3)
y +2

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,171 ratings)

Output:

R Data Types1

The above statement gives the output by using the c() function, which adds variable t to 2.

  • The length of the vector is calculated by the len () function.

Code:

len (y)

Output:

R Data Types2

  • Logical Vectors: Comparison of two numbers with logical values like True, false, NA. Logical operators to satisfy certain conditions include <, <=, >, >=, ==, != for inequality.

Example #1

Code:

v <- seq ( -2 ,2)
l < -v > 0
l

Output:

R Data Types3

Example #2

Code:

x=c (3,6,1,2)
x>2

Output:

R Data Types4

Code:

rep () – to create replicate values.
rep(1,3)
rep( 3:6 ,2)
rep( 1:3 , each =2)
rep(1:3 , times=2 ,each =2)

  • Create a Vector

Code:

color <- c (‘blue’, ‘pink’, ‘white’)
print (color)

  • To display the class of the vector

Code:

print ((class (color))

Output:

r data5

In the above program [1], this one denotes the first element of the vector.

2. Factor

The factor adds numeric codes along with the character level. In simple, it defines categorical data with ordered and unordered sets. They are defined using function factor (). Storing data in a factor helps to store data efficiently in statistical modeling.

Example #1

Code:

f = factor (c(1, 6,2,4,7,1,6,7,8)
print (f)

Output:

r data6

Example #2

Code:

k = factor (c( 2,0,2,0,0,0 ) , levels =c(0,2) , labels =c( “ prince “ ,”princess”))
k

Output:

r data7

3. Matrix

In R programming matrix is a two-dimensional element with numeric and character vectors, simply an atomic vector with the number of rows and columns. Three ways to create a matrix are using the function matrix(), converting the vector into the matrix, and binding vectors. Some functions useful here are :

  • rbind() and cbind(): combines or binds columns and rows.
  • dim(): setting dimensions.

Syntax:

variable <- matrix(vector, n rows, n columns, split by row or column)

Here if it is true, it splits by row, false returns split by columns.

Example #1

  • Consider a matrix.

Code:

x = matrix(c (1,2,3,4,5,6,7,8) 2,4, true)
print (x)

Output:

r data8

Example #2

  • Considering Bind.

Code:

a <- 1:4
b<- 10 :13
cbind( a,b)
a   b

Output:

r data9

4. List

List stores Objects and the elements can be a character, matrices, arrays, numeric. it may consist of another list as an item too.

Syntax:

variable <- list (list items)

Example of an R list:

Code:

lak = list (23, “hi”, cos, list (5L,” l”))
print (lak)

Output:

r data10

Example Considering copies of three Vectors:

Code:

a =c(3,5,6)
b =c(“aa”,”cc”,”ee”)
x=c (true, false, true)
y=list(a,b,x)

Therefore y holds the copies of a,b,x.

5. Data Frame

Data frames are two-dimensional with a group of vectors by an equal length. It’s a special kind of list with a rectangular format list. The key factor is to store data tables. They are created using function data. the frame ().

Syntax:

variable <- data.frame ( list 1, list 2… list N)

Example #1

Let’s see an example of the data frame In R.

Code:

X= data.frame( values =c(20,50,10), name =c(‘ Gri’,’Tom’,’jeff’))
print(X) values Name

Output:

r data11

Even we can use built-in data frames. In which the top element defines a header, followed by data rows and columns. To see the preview, we can use the head function before.

Example #2

Code:

computer
Date  intel    speed   data
hp   1990    8081   MHZ    8
acer    2001    80286 Mhz     16

To define the class of the intel:

computer [[‘intel’]]

Output:

r data12

Conclusion

In this article, we have gone through different R data types that are used in programming. To make any application, we need variables to store the values, and all these variables are necessary to assign data types. These data types are used in data analysis. Understanding data types help while debugging for computational purposes.

Recommended Articles

This is a guide to R Data Types. Here we discuss different types of R data with various examples to assign data types. You can also go through our other related articles to learn more –

  1. R Data Frame
  2. C++ Data Types
  3. PL/SQL Data Types
  4. SQL Server Data Types
Popular Course in this category
R Programming Training (13 Courses, 20+ Projects)
  13 Online Courses |  20 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Statistical Analysis Training (15 Courses, 10+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more