SlideShare a Scribd company logo
1 of 14
Download to read offline
Data types and Structures
in R
Rupak Roy
 Data type defines what sort of value it is. The very most commonly
used data types are numbers which is called as numeric values in R
and text which is again a character value.
 Data Structures can be defined as the structure of storing the data.
 Some of the common data structures
Vectors: a collection of values that has all the same data type.
The elements of a vector can be numeric vector or a
character vector. A vector can also be used to represent
a single variable in a data set .
Data Types
Rupak Roy
Factors: are also a collection of variables that are used to
categorize the data and store it as levels. It is similar to a
vector except they can store both strings and integers.
For example : number of gear types in the column.
Factor w/ 3 levels "3","4","5": 2 2 2 1 1 1 1 2 2 2 . means it has
a list of three types of gears i.e. 3, 4 and 5
Data Structures: factors
Rupak Roy
 Matrices: a two dimensional collection of values where all have
the same data type. The values are arranged
in rows and columns, used when the
data is a high dimensional array.
#creating a matrix and saving it as a R object
> A=matrix( c( 1,2,3,3,2,1), nrow= 3, ncol = 2)
Now we can access the elements using:
> A[1, ] #i.e. [row,column] Output=> 13
> A[ ,2] #i.e. [ , 2nd column] Output=> 321
> A[3,2] #i.e.[3rd row,2nd column] Output => 1
Data Structures: matrices
Rupak Roy
Data Structures: data frame
 Data frame: it is like a single table with rows and columns of same or different
data types.
 Let’s see how to create a data frame.
#vectors
> Student=c(1,2,3,4,5,6)
> pre_module_score=c(18,21,23,22,24,17)
> post_module_score=c(22,21,24,15,18,19)
> module_name=c( "graded", "graded", "non-graded",
"graded", "non-graded", "non-graded")
> test_scores= data.frame(Student,pre_module_score,post_module_score,
module_name)
Alternatively;
> test_scores=data.frame(Student=c(1,2,3,4,5,6), pre_module_score=c(18,21,23,22,24,17),
pos_module_score=c(22,21,24,15,18,19), module_name=c("graded","graded","non-
graded", "graded","non-graded","non- graded"))
 List: is a collection of objects of same or different data types.
#list
We can access
a list by its list
Position
Like list [[1]]
[1] bob
Data Structures: list
Rupak Roy
#dataframe: a table with rows and columns
#take the following vectors
> subject=c("geography","history","chemistry")
> testscores=c(77,61,65)
> remarks = c("very good","average","good") > Markscard =
> data.frame(subject,testscores,remarks)
> Markscard
OR
> Markscard<-data.frame(subject = c("geography","history","chemistry"),
testscores=c(77,61,65),remarks = c("very good","average","good"))
Examples in R
Rupak Roy
 To know what is the type of data structure the Markscard is use:
> class(Markscard)
And to access a element from the table or data.frame
> Markscard [,3] #i.e. Markscard [ row , column]
> Markscard[3,]
Let’s load the in-build R studio datasets.
>library(datasets)
#from the list of datasets we will call Iris dataset
>data(iris)
Examples in R
Rupak Roy
#to view the column names
> names(iris) or colnames(iris)
#to view the data structure of the iris data set
> str(iris)
#to know the dimensions of the iris data set
> dim(iris)
150 rows and 5 columns
#to view the number of rows and columns
> nrow(iris) and > ncol(iris)
Examples in R
Rupak Roy
Examples in R
#to view the whole dataset.
>view(iris)
#to view the top or the bottom most values from the dataset.
> head(iris)
> Tail(iris)
#to view only few top or the bottom most values from the dataset.
> head(iris,10) > tail(iris,10)
#to know more about head, tail function or any functions use:
> ?head
#to view a range of rows or columns from the dataset use
> iris[15:20,] #i.e. from rows 15 to 20
> iris[15:20,2:3] #i.e. from rows 15 to 20 and column 2 and 3
Rupak Roy
#we can access all the values of a particular column using $
> iris$Species
#Or we can access a particular value from the column using:
>iris$Species[3] i.e. the 3rd row of species column
#Else a range of values/rows from a particular column using:
> iris$Species[50:10] i.e. from row 50 to 100
#to have a quick summary of the dataset use:
> summary(iris)
#we can also check the data type of a variable using:
> is.character(iris$Species)
> is.numeric(iris$PetalLength)
Examples in R
Rupak Roy
Examples in R
#we can use the ‘attach’ function to take all the columns of iris
data set and create an individual objects so that we don’t have
to use $ to call the columns of the dataset.
> attach(iris)
> Species #‘Species’ previously accessed by using iris$Species
#it is better to detach the dataset after we are done with the
dataset as we are aware that R uses systems RAM to perform
its tasks.
> detach(iris)
Rupak Roy
#we can view the working directory of R by
> getwd()
#we also set the work directory by our choice by
> setwd(“c:/Users/data2dimensions/documents”)
#it is preferable to use:
> gc() i.e. garbage collection (GC) automatically releases memory
when an object is no longer used. It does this by tracking how many names
point to each object and when there are no names pointing to an object, it
deletes that object.
Examples in R
Rupak Roy
 Next :
We will learn how to import and export data in R
Data types and structures
Rupak Roy

More Related Content

What's hot

Exploratory data analysis in R - Data Science Club
Exploratory data analysis in R - Data Science ClubExploratory data analysis in R - Data Science Club
Exploratory data analysis in R - Data Science ClubMartin Bago
 
R programming slides
R  programming slidesR  programming slides
R programming slidesPankaj Saini
 
Data visualization using R
Data visualization using RData visualization using R
Data visualization using RUmmiya Mohammedi
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programmingNimrita Koul
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Guy Lebanon
 
Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2izahn
 
Data analysis with R
Data analysis with RData analysis with R
Data analysis with RShareThis
 
R language tutorial
R language tutorialR language tutorial
R language tutorialDavid Chiu
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programmingAlberto Labarga
 
R data-import, data-export
R data-import, data-exportR data-import, data-export
R data-import, data-exportFAO
 
R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to MatricesRsquared Academy
 
Chart and graphs in R programming language
Chart and graphs in R programming language Chart and graphs in R programming language
Chart and graphs in R programming language CHANDAN KUMAR
 
R basics
R basicsR basics
R basicsFAO
 
Import and Export Big Data using R Studio
Import and Export Big Data using R StudioImport and Export Big Data using R Studio
Import and Export Big Data using R StudioRupak Roy
 
R programming Fundamentals
R programming  FundamentalsR programming  Fundamentals
R programming FundamentalsRagia Ibrahim
 

What's hot (20)

Exploratory data analysis in R - Data Science Club
Exploratory data analysis in R - Data Science ClubExploratory data analysis in R - Data Science Club
Exploratory data analysis in R - Data Science Club
 
R studio
R studio R studio
R studio
 
Unit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptxUnit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptx
 
Data Management in R
Data Management in RData Management in R
Data Management in R
 
Step By Step Guide to Learn R
Step By Step Guide to Learn RStep By Step Guide to Learn R
Step By Step Guide to Learn R
 
R programming slides
R  programming slidesR  programming slides
R programming slides
 
Data visualization using R
Data visualization using RData visualization using R
Data visualization using R
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)
 
Class ppt intro to r
Class ppt intro to rClass ppt intro to r
Class ppt intro to r
 
Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2
 
Data analysis with R
Data analysis with RData analysis with R
Data analysis with R
 
R language tutorial
R language tutorialR language tutorial
R language tutorial
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programming
 
R data-import, data-export
R data-import, data-exportR data-import, data-export
R data-import, data-export
 
R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to Matrices
 
Chart and graphs in R programming language
Chart and graphs in R programming language Chart and graphs in R programming language
Chart and graphs in R programming language
 
R basics
R basicsR basics
R basics
 
Import and Export Big Data using R Studio
Import and Export Big Data using R StudioImport and Export Big Data using R Studio
Import and Export Big Data using R Studio
 
R programming Fundamentals
R programming  FundamentalsR programming  Fundamentals
R programming Fundamentals
 

Similar to Data Types and Structures in R

Similar to Data Types and Structures in R (20)

Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Array
ArrayArray
Array
 
Variables & Data Types in R
Variables & Data Types in RVariables & Data Types in R
Variables & Data Types in R
 
R교육1
R교육1R교육1
R교육1
 
arrays.docx
arrays.docxarrays.docx
arrays.docx
 
23. SQL and Database.pdf
23. SQL and Database.pdf23. SQL and Database.pdf
23. SQL and Database.pdf
 
23. SQL and Database.pdf
23. SQL and Database.pdf23. SQL and Database.pdf
23. SQL and Database.pdf
 
Array
ArrayArray
Array
 
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptxfINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
 
Array
ArrayArray
Array
 
Data types in r
Data types in rData types in r
Data types in r
 
R Programming Tutorial for Beginners - -TIB Academy
R Programming Tutorial for Beginners - -TIB AcademyR Programming Tutorial for Beginners - -TIB Academy
R Programming Tutorial for Beginners - -TIB Academy
 
Arrays
ArraysArrays
Arrays
 
R data types
R   data typesR   data types
R data types
 
R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptx
 
Ggplot2 work
Ggplot2 workGgplot2 work
Ggplot2 work
 
Array 2 hina
Array 2 hina Array 2 hina
Array 2 hina
 
Ch08
Ch08Ch08
Ch08
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
arrays-130116232821-phpapp02.pdf
arrays-130116232821-phpapp02.pdfarrays-130116232821-phpapp02.pdf
arrays-130116232821-phpapp02.pdf
 

More from Rupak Roy

Hierarchical Clustering - Text Mining/NLP
Hierarchical Clustering - Text Mining/NLPHierarchical Clustering - Text Mining/NLP
Hierarchical Clustering - Text Mining/NLPRupak Roy
 
Clustering K means and Hierarchical - NLP
Clustering K means and Hierarchical - NLPClustering K means and Hierarchical - NLP
Clustering K means and Hierarchical - NLPRupak Roy
 
Network Analysis - NLP
Network Analysis  - NLPNetwork Analysis  - NLP
Network Analysis - NLPRupak Roy
 
Topic Modeling - NLP
Topic Modeling - NLPTopic Modeling - NLP
Topic Modeling - NLPRupak Roy
 
Sentiment Analysis Practical Steps
Sentiment Analysis Practical StepsSentiment Analysis Practical Steps
Sentiment Analysis Practical StepsRupak Roy
 
NLP - Sentiment Analysis
NLP - Sentiment AnalysisNLP - Sentiment Analysis
NLP - Sentiment AnalysisRupak Roy
 
Text Mining using Regular Expressions
Text Mining using Regular ExpressionsText Mining using Regular Expressions
Text Mining using Regular ExpressionsRupak Roy
 
Introduction to Text Mining
Introduction to Text Mining Introduction to Text Mining
Introduction to Text Mining Rupak Roy
 
Apache Hbase Architecture
Apache Hbase ArchitectureApache Hbase Architecture
Apache Hbase ArchitectureRupak Roy
 
Introduction to Hbase
Introduction to Hbase Introduction to Hbase
Introduction to Hbase Rupak Roy
 
Apache Hive Table Partition and HQL
Apache Hive Table Partition and HQLApache Hive Table Partition and HQL
Apache Hive Table Partition and HQLRupak Roy
 
Installing Apache Hive, internal and external table, import-export
Installing Apache Hive, internal and external table, import-export Installing Apache Hive, internal and external table, import-export
Installing Apache Hive, internal and external table, import-export Rupak Roy
 
Introductive to Hive
Introductive to Hive Introductive to Hive
Introductive to Hive Rupak Roy
 
Scoop Job, import and export to RDBMS
Scoop Job, import and export to RDBMSScoop Job, import and export to RDBMS
Scoop Job, import and export to RDBMSRupak Roy
 
Apache Scoop - Import with Append mode and Last Modified mode
Apache Scoop - Import with Append mode and Last Modified mode Apache Scoop - Import with Append mode and Last Modified mode
Apache Scoop - Import with Append mode and Last Modified mode Rupak Roy
 
Introduction to scoop and its functions
Introduction to scoop and its functionsIntroduction to scoop and its functions
Introduction to scoop and its functionsRupak Roy
 
Introduction to Flume
Introduction to FlumeIntroduction to Flume
Introduction to FlumeRupak Roy
 
Apache Pig Relational Operators - II
Apache Pig Relational Operators - II Apache Pig Relational Operators - II
Apache Pig Relational Operators - II Rupak Roy
 
Passing Parameters using File and Command Line
Passing Parameters using File and Command LinePassing Parameters using File and Command Line
Passing Parameters using File and Command LineRupak Roy
 
Apache PIG Relational Operations
Apache PIG Relational Operations Apache PIG Relational Operations
Apache PIG Relational Operations Rupak Roy
 

More from Rupak Roy (20)

Hierarchical Clustering - Text Mining/NLP
Hierarchical Clustering - Text Mining/NLPHierarchical Clustering - Text Mining/NLP
Hierarchical Clustering - Text Mining/NLP
 
Clustering K means and Hierarchical - NLP
Clustering K means and Hierarchical - NLPClustering K means and Hierarchical - NLP
Clustering K means and Hierarchical - NLP
 
Network Analysis - NLP
Network Analysis  - NLPNetwork Analysis  - NLP
Network Analysis - NLP
 
Topic Modeling - NLP
Topic Modeling - NLPTopic Modeling - NLP
Topic Modeling - NLP
 
Sentiment Analysis Practical Steps
Sentiment Analysis Practical StepsSentiment Analysis Practical Steps
Sentiment Analysis Practical Steps
 
NLP - Sentiment Analysis
NLP - Sentiment AnalysisNLP - Sentiment Analysis
NLP - Sentiment Analysis
 
Text Mining using Regular Expressions
Text Mining using Regular ExpressionsText Mining using Regular Expressions
Text Mining using Regular Expressions
 
Introduction to Text Mining
Introduction to Text Mining Introduction to Text Mining
Introduction to Text Mining
 
Apache Hbase Architecture
Apache Hbase ArchitectureApache Hbase Architecture
Apache Hbase Architecture
 
Introduction to Hbase
Introduction to Hbase Introduction to Hbase
Introduction to Hbase
 
Apache Hive Table Partition and HQL
Apache Hive Table Partition and HQLApache Hive Table Partition and HQL
Apache Hive Table Partition and HQL
 
Installing Apache Hive, internal and external table, import-export
Installing Apache Hive, internal and external table, import-export Installing Apache Hive, internal and external table, import-export
Installing Apache Hive, internal and external table, import-export
 
Introductive to Hive
Introductive to Hive Introductive to Hive
Introductive to Hive
 
Scoop Job, import and export to RDBMS
Scoop Job, import and export to RDBMSScoop Job, import and export to RDBMS
Scoop Job, import and export to RDBMS
 
Apache Scoop - Import with Append mode and Last Modified mode
Apache Scoop - Import with Append mode and Last Modified mode Apache Scoop - Import with Append mode and Last Modified mode
Apache Scoop - Import with Append mode and Last Modified mode
 
Introduction to scoop and its functions
Introduction to scoop and its functionsIntroduction to scoop and its functions
Introduction to scoop and its functions
 
Introduction to Flume
Introduction to FlumeIntroduction to Flume
Introduction to Flume
 
Apache Pig Relational Operators - II
Apache Pig Relational Operators - II Apache Pig Relational Operators - II
Apache Pig Relational Operators - II
 
Passing Parameters using File and Command Line
Passing Parameters using File and Command LinePassing Parameters using File and Command Line
Passing Parameters using File and Command Line
 
Apache PIG Relational Operations
Apache PIG Relational Operations Apache PIG Relational Operations
Apache PIG Relational Operations
 

Recently uploaded

AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 

Recently uploaded (20)

AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 

Data Types and Structures in R

  • 1. Data types and Structures in R Rupak Roy
  • 2.  Data type defines what sort of value it is. The very most commonly used data types are numbers which is called as numeric values in R and text which is again a character value.  Data Structures can be defined as the structure of storing the data.  Some of the common data structures Vectors: a collection of values that has all the same data type. The elements of a vector can be numeric vector or a character vector. A vector can also be used to represent a single variable in a data set . Data Types Rupak Roy
  • 3. Factors: are also a collection of variables that are used to categorize the data and store it as levels. It is similar to a vector except they can store both strings and integers. For example : number of gear types in the column. Factor w/ 3 levels "3","4","5": 2 2 2 1 1 1 1 2 2 2 . means it has a list of three types of gears i.e. 3, 4 and 5 Data Structures: factors Rupak Roy
  • 4.  Matrices: a two dimensional collection of values where all have the same data type. The values are arranged in rows and columns, used when the data is a high dimensional array. #creating a matrix and saving it as a R object > A=matrix( c( 1,2,3,3,2,1), nrow= 3, ncol = 2) Now we can access the elements using: > A[1, ] #i.e. [row,column] Output=> 13 > A[ ,2] #i.e. [ , 2nd column] Output=> 321 > A[3,2] #i.e.[3rd row,2nd column] Output => 1 Data Structures: matrices Rupak Roy
  • 5. Data Structures: data frame  Data frame: it is like a single table with rows and columns of same or different data types.  Let’s see how to create a data frame. #vectors > Student=c(1,2,3,4,5,6) > pre_module_score=c(18,21,23,22,24,17) > post_module_score=c(22,21,24,15,18,19) > module_name=c( "graded", "graded", "non-graded", "graded", "non-graded", "non-graded") > test_scores= data.frame(Student,pre_module_score,post_module_score, module_name) Alternatively; > test_scores=data.frame(Student=c(1,2,3,4,5,6), pre_module_score=c(18,21,23,22,24,17), pos_module_score=c(22,21,24,15,18,19), module_name=c("graded","graded","non- graded", "graded","non-graded","non- graded"))
  • 6.  List: is a collection of objects of same or different data types. #list We can access a list by its list Position Like list [[1]] [1] bob Data Structures: list Rupak Roy
  • 7. #dataframe: a table with rows and columns #take the following vectors > subject=c("geography","history","chemistry") > testscores=c(77,61,65) > remarks = c("very good","average","good") > Markscard = > data.frame(subject,testscores,remarks) > Markscard OR > Markscard<-data.frame(subject = c("geography","history","chemistry"), testscores=c(77,61,65),remarks = c("very good","average","good")) Examples in R Rupak Roy
  • 8.  To know what is the type of data structure the Markscard is use: > class(Markscard) And to access a element from the table or data.frame > Markscard [,3] #i.e. Markscard [ row , column] > Markscard[3,] Let’s load the in-build R studio datasets. >library(datasets) #from the list of datasets we will call Iris dataset >data(iris) Examples in R Rupak Roy
  • 9. #to view the column names > names(iris) or colnames(iris) #to view the data structure of the iris data set > str(iris) #to know the dimensions of the iris data set > dim(iris) 150 rows and 5 columns #to view the number of rows and columns > nrow(iris) and > ncol(iris) Examples in R Rupak Roy
  • 10. Examples in R #to view the whole dataset. >view(iris) #to view the top or the bottom most values from the dataset. > head(iris) > Tail(iris) #to view only few top or the bottom most values from the dataset. > head(iris,10) > tail(iris,10) #to know more about head, tail function or any functions use: > ?head #to view a range of rows or columns from the dataset use > iris[15:20,] #i.e. from rows 15 to 20 > iris[15:20,2:3] #i.e. from rows 15 to 20 and column 2 and 3 Rupak Roy
  • 11. #we can access all the values of a particular column using $ > iris$Species #Or we can access a particular value from the column using: >iris$Species[3] i.e. the 3rd row of species column #Else a range of values/rows from a particular column using: > iris$Species[50:10] i.e. from row 50 to 100 #to have a quick summary of the dataset use: > summary(iris) #we can also check the data type of a variable using: > is.character(iris$Species) > is.numeric(iris$PetalLength) Examples in R Rupak Roy
  • 12. Examples in R #we can use the ‘attach’ function to take all the columns of iris data set and create an individual objects so that we don’t have to use $ to call the columns of the dataset. > attach(iris) > Species #‘Species’ previously accessed by using iris$Species #it is better to detach the dataset after we are done with the dataset as we are aware that R uses systems RAM to perform its tasks. > detach(iris) Rupak Roy
  • 13. #we can view the working directory of R by > getwd() #we also set the work directory by our choice by > setwd(“c:/Users/data2dimensions/documents”) #it is preferable to use: > gc() i.e. garbage collection (GC) automatically releases memory when an object is no longer used. It does this by tracking how many names point to each object and when there are no names pointing to an object, it deletes that object. Examples in R Rupak Roy
  • 14.  Next : We will learn how to import and export data in R Data types and structures Rupak Roy