SlideShare a Scribd company logo
1 of 7
Download to read offline
© 2012 Heather Turner
Part I
Writing R Code
5 / 80
© 2012 Heather Turner
Data Structures
Data structures are the building blocks of code. In R
there are four main types of structure:
• vectors and factors
• matrices and arrays
• lists
• data frames
6 / 80
© 2012 Heather Turner
Vectors
Vectors are one dimensional data objects
> c(1, 2, 3)
[1] 1 2 3
The elements must all be of the same type: logical,
numeric or character, and will be coerced if necessary:
> c(NA, FALSE, 1)
[1] NA 0 1
> c(NA, TRUE, 1, "a")
[1] NA "TRUE" "1" "a"
The functions as.logical, as.numeric and as.character
will coerce to the corresponding type, producing NAs
when coercion fails.
7 / 80
© 2012 Heather Turner
Logical Vectors
Logical vectors are commonly used for indexing
> x <- c(1, 1, 2, 2)
> x[x > 1]
[1] 2 2
The logical binary operators are >, <, ==, <=, >= and !=,
whilst ! is a unary operator. Some functions also return
logical vectors, e.g. duplicated:
> duplicated(x)
[1] FALSE TRUE FALSE TRUE
> !duplicated(x)
[1] TRUE FALSE TRUE FALSE
Single element logical vectors (scalars) are useful for
flow control, see later.
8 / 80
© 2012 Heather Turner
Numeric Vectors
The are several convenience function for creating
numeric vectors, the most common being seq and rep.
As they are so useful there are fast shortcuts for
particular cases
> x <- 3:5
> seq_len(3)
[1] 1 2 3
> seq_along(x)
[1] 1 2 3
> rep.int(1:2, times = c(2, 3))
[1] 1 1 2 2 2
9 / 80
© 2012 Heather Turner
Character Vectors
Character vectors may be used for creating names
> names(x) <- paste(LETTERS[1:3], 1229:1231, sep = "")
> x
A1229 B1230 C1231
3 4 5
Useful functions to extract information from character
vectors include nchar, substr, strsplit, grep and gsub.
The last two are for pattern matching and replacement
> grep("123", names(x), fixed = TRUE)
[1] 2 3
> as.numeric(gsub("[A-C]", "", names(x)))
[1] 1229 1230 1231
The latter uses a regular expression, see references.
10 / 80
© 2012 Heather Turner
Factors
Factors are a special type of vector representing a
categorical variable as a vector of numeric codes with
an attribute giving the levels
> colour <- factor(rep(c("red", "green", "blue"), 2))
> str(colour)
Factor w/ 3 levels "blue","green",..: 3 2 1 3 2 1
The function gl is useful for creating factors
> gl(3, 1, length = 6, labels = c("blue", "green", "red"))
[1] blue green red blue green red
Levels: blue green red
> gl(3, 2, length = 6, labels = c("red", "green", "blue"))
[1] red red green green blue blue
Levels: red green blue
11 / 80

More Related Content

What's hot

Grds international conference on pure and applied science (9)
Grds international conference on pure and applied science (9)Grds international conference on pure and applied science (9)
Grds international conference on pure and applied science (9)Global R & D Services
 
Introductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueIntroductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueGhaffar Khan
 
Introduction to Data Structure
Introduction to Data Structure Introduction to Data Structure
Introduction to Data Structure Kamal Singh Lodhi
 
Matrix and its operation (addition, subtraction, multiplication)
Matrix and its operation (addition, subtraction, multiplication)Matrix and its operation (addition, subtraction, multiplication)
Matrix and its operation (addition, subtraction, multiplication)NirnayMukharjee
 
WCS Specialist Maths An Introduction to Matrices PowerPoint
WCS Specialist Maths An Introduction to Matrices PowerPointWCS Specialist Maths An Introduction to Matrices PowerPoint
WCS Specialist Maths An Introduction to Matrices PowerPointKingp18
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST Kathirvel Ayyaswamy
 
9.4 Cramer's Rule
9.4 Cramer's Rule9.4 Cramer's Rule
9.4 Cramer's Rulesmiller5
 
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data StructuresStacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data StructuresAmrinder Arora
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1Kumar
 
Introduction To Data Structures.
Introduction To Data Structures.Introduction To Data Structures.
Introduction To Data Structures.Education Front
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsAakash deep Singhal
 

What's hot (20)

Grds international conference on pure and applied science (9)
Grds international conference on pure and applied science (9)Grds international conference on pure and applied science (9)
Grds international conference on pure and applied science (9)
 
Linear discriminant analysis
Linear discriminant analysisLinear discriminant analysis
Linear discriminant analysis
 
Lect20
Lect20Lect20
Lect20
 
Introductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueIntroductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, Queue
 
Introduction to Data Structure
Introduction to Data Structure Introduction to Data Structure
Introduction to Data Structure
 
Matrix and its operation (addition, subtraction, multiplication)
Matrix and its operation (addition, subtraction, multiplication)Matrix and its operation (addition, subtraction, multiplication)
Matrix and its operation (addition, subtraction, multiplication)
 
Calculus
CalculusCalculus
Calculus
 
Array
ArrayArray
Array
 
Mean, median, mode
Mean, median, mode Mean, median, mode
Mean, median, mode
 
Mean, median, mode 1
Mean, median, mode 1Mean, median, mode 1
Mean, median, mode 1
 
Lect19
Lect19Lect19
Lect19
 
Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searching
 
Lecture3a sorting
Lecture3a sortingLecture3a sorting
Lecture3a sorting
 
WCS Specialist Maths An Introduction to Matrices PowerPoint
WCS Specialist Maths An Introduction to Matrices PowerPointWCS Specialist Maths An Introduction to Matrices PowerPoint
WCS Specialist Maths An Introduction to Matrices PowerPoint
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST
 
9.4 Cramer's Rule
9.4 Cramer's Rule9.4 Cramer's Rule
9.4 Cramer's Rule
 
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data StructuresStacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1
 
Introduction To Data Structures.
Introduction To Data Structures.Introduction To Data Structures.
Introduction To Data Structures.
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 

Viewers also liked

Sample slides from "Getting Started with R" course
Sample slides from "Getting Started with R" courseSample slides from "Getting Started with R" course
Sample slides from "Getting Started with R" coursehtstatistics
 
Detecting Drug Effects in the Brain
Detecting Drug Effects in the BrainDetecting Drug Effects in the Brain
Detecting Drug Effects in the Brainhtstatistics
 
Clustering Microarray Data
Clustering Microarray DataClustering Microarray Data
Clustering Microarray Datahtstatistics
 
From L to N: Nonlinear Predictors in Generalized Models
From L to N: Nonlinear Predictors in Generalized ModelsFrom L to N: Nonlinear Predictors in Generalized Models
From L to N: Nonlinear Predictors in Generalized Modelshtstatistics
 
Custom Functions for Specifying Nonlinear Terms to gnm
Custom Functions for Specifying Nonlinear Terms to gnmCustom Functions for Specifying Nonlinear Terms to gnm
Custom Functions for Specifying Nonlinear Terms to gnmhtstatistics
 
Modelling the Diluting Effect of Social Mobility on Health Inequality
Modelling the Diluting Effect of Social Mobility on Health InequalityModelling the Diluting Effect of Social Mobility on Health Inequality
Modelling the Diluting Effect of Social Mobility on Health Inequalityhtstatistics
 
gnm: a Package for Generalized Nonlinear Models
gnm: a Package for Generalized Nonlinear Modelsgnm: a Package for Generalized Nonlinear Models
gnm: a Package for Generalized Nonlinear Modelshtstatistics
 
Nonlinear Discrete-time Hazard Models for Entry into Marriage
Nonlinear Discrete-time Hazard Models for Entry into MarriageNonlinear Discrete-time Hazard Models for Entry into Marriage
Nonlinear Discrete-time Hazard Models for Entry into Marriagehtstatistics
 
BradleyTerry2: Flexible Models for Paired Comparisons
BradleyTerry2: Flexible Models for Paired ComparisonsBradleyTerry2: Flexible Models for Paired Comparisons
BradleyTerry2: Flexible Models for Paired Comparisonshtstatistics
 
Generalized Bradley-Terry Modelling of Football Results
Generalized Bradley-Terry Modelling of Football ResultsGeneralized Bradley-Terry Modelling of Football Results
Generalized Bradley-Terry Modelling of Football Resultshtstatistics
 
Multiplicative Interaction Models in R
Multiplicative Interaction Models in RMultiplicative Interaction Models in R
Multiplicative Interaction Models in Rhtstatistics
 
√23 spain 2
√23 spain 2√23 spain 2
√23 spain 2heightses
 
Dont Build Another Tower of Babel
Dont Build Another Tower of BabelDont Build Another Tower of Babel
Dont Build Another Tower of Babelecomba
 

Viewers also liked (15)

Sample slides from "Getting Started with R" course
Sample slides from "Getting Started with R" courseSample slides from "Getting Started with R" course
Sample slides from "Getting Started with R" course
 
Detecting Drug Effects in the Brain
Detecting Drug Effects in the BrainDetecting Drug Effects in the Brain
Detecting Drug Effects in the Brain
 
Clustering Microarray Data
Clustering Microarray DataClustering Microarray Data
Clustering Microarray Data
 
From L to N: Nonlinear Predictors in Generalized Models
From L to N: Nonlinear Predictors in Generalized ModelsFrom L to N: Nonlinear Predictors in Generalized Models
From L to N: Nonlinear Predictors in Generalized Models
 
Custom Functions for Specifying Nonlinear Terms to gnm
Custom Functions for Specifying Nonlinear Terms to gnmCustom Functions for Specifying Nonlinear Terms to gnm
Custom Functions for Specifying Nonlinear Terms to gnm
 
Modelling the Diluting Effect of Social Mobility on Health Inequality
Modelling the Diluting Effect of Social Mobility on Health InequalityModelling the Diluting Effect of Social Mobility on Health Inequality
Modelling the Diluting Effect of Social Mobility on Health Inequality
 
gnm: a Package for Generalized Nonlinear Models
gnm: a Package for Generalized Nonlinear Modelsgnm: a Package for Generalized Nonlinear Models
gnm: a Package for Generalized Nonlinear Models
 
Nonlinear Discrete-time Hazard Models for Entry into Marriage
Nonlinear Discrete-time Hazard Models for Entry into MarriageNonlinear Discrete-time Hazard Models for Entry into Marriage
Nonlinear Discrete-time Hazard Models for Entry into Marriage
 
BradleyTerry2: Flexible Models for Paired Comparisons
BradleyTerry2: Flexible Models for Paired ComparisonsBradleyTerry2: Flexible Models for Paired Comparisons
BradleyTerry2: Flexible Models for Paired Comparisons
 
Generalized Bradley-Terry Modelling of Football Results
Generalized Bradley-Terry Modelling of Football ResultsGeneralized Bradley-Terry Modelling of Football Results
Generalized Bradley-Terry Modelling of Football Results
 
Multiplicative Interaction Models in R
Multiplicative Interaction Models in RMultiplicative Interaction Models in R
Multiplicative Interaction Models in R
 
Kepelbagaian pelajar
Kepelbagaian pelajarKepelbagaian pelajar
Kepelbagaian pelajar
 
√23 spain 2
√23 spain 2√23 spain 2
√23 spain 2
 
Cert..
Cert..Cert..
Cert..
 
Dont Build Another Tower of Babel
Dont Build Another Tower of BabelDont Build Another Tower of Babel
Dont Build Another Tower of Babel
 

Similar to Sample slides from "Programming with R" course

R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environmentYogendra Chaubey
 
Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017Parth Khare
 
[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2Kevin Chun-Hsien Hsu
 
Basic operations by novi reandy sasmita
Basic operations by novi reandy sasmitaBasic operations by novi reandy sasmita
Basic operations by novi reandy sasmitabeasiswa
 
Day 1d R structures & objects: matrices and data frames.pptx
Day 1d   R structures & objects: matrices and data frames.pptxDay 1d   R structures & objects: matrices and data frames.pptx
Day 1d R structures & objects: matrices and data frames.pptxAdrien Melquiond
 
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 Academyrajkamaltibacademy
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data ManipulationChu An
 
Day 1c access, select ordering copy.pptx
Day 1c   access, select   ordering copy.pptxDay 1c   access, select   ordering copy.pptx
Day 1c access, select ordering copy.pptxAdrien Melquiond
 
R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptxkalai75
 

Similar to Sample slides from "Programming with R" course (20)

R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environment
 
R Basics
R BasicsR Basics
R Basics
 
R교육1
R교육1R교육1
R교육1
 
R learning by examples
R learning by examplesR learning by examples
R learning by examples
 
Language R
Language RLanguage R
Language R
 
Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017
 
Programming in R
Programming in RProgramming in R
Programming in R
 
Vectors.pptx
Vectors.pptxVectors.pptx
Vectors.pptx
 
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
 
[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2
 
Basic operations by novi reandy sasmita
Basic operations by novi reandy sasmitaBasic operations by novi reandy sasmita
Basic operations by novi reandy sasmita
 
R tutorial (R program 101)
R tutorial (R program 101)R tutorial (R program 101)
R tutorial (R program 101)
 
Day 1d R structures & objects: matrices and data frames.pptx
Day 1d   R structures & objects: matrices and data frames.pptxDay 1d   R structures & objects: matrices and data frames.pptx
Day 1d R structures & objects: matrices and data frames.pptx
 
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
 
R programming
R programmingR programming
R programming
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
bobok
bobokbobok
bobok
 
Day 1c access, select ordering copy.pptx
Day 1c   access, select   ordering copy.pptxDay 1c   access, select   ordering copy.pptx
Day 1c access, select ordering copy.pptx
 
R Programming Homework Help
R Programming Homework HelpR Programming Homework Help
R Programming Homework Help
 
R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptx
 

Recently uploaded

꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Callshivangimorya083
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxFurkanTasci3
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxStephen266013
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
Data Warehouse , Data Cube Computation
Data Warehouse   , Data Cube ComputationData Warehouse   , Data Cube Computation
Data Warehouse , Data Cube Computationsit20ad004
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...shivangimorya083
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...Suhani Kapoor
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSAishani27
 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...Suhani Kapoor
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
Data Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health ClassificationData Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health ClassificationBoston Institute of Analytics
 

Recently uploaded (20)

꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptx
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docx
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
Data Warehouse , Data Cube Computation
Data Warehouse   , Data Cube ComputationData Warehouse   , Data Cube Computation
Data Warehouse , Data Cube Computation
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICS
 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
Data Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health ClassificationData Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health Classification
 

Sample slides from "Programming with R" course

  • 1. © 2012 Heather Turner Part I Writing R Code 5 / 80
  • 2. © 2012 Heather Turner Data Structures Data structures are the building blocks of code. In R there are four main types of structure: • vectors and factors • matrices and arrays • lists • data frames 6 / 80
  • 3. © 2012 Heather Turner Vectors Vectors are one dimensional data objects > c(1, 2, 3) [1] 1 2 3 The elements must all be of the same type: logical, numeric or character, and will be coerced if necessary: > c(NA, FALSE, 1) [1] NA 0 1 > c(NA, TRUE, 1, "a") [1] NA "TRUE" "1" "a" The functions as.logical, as.numeric and as.character will coerce to the corresponding type, producing NAs when coercion fails. 7 / 80
  • 4. © 2012 Heather Turner Logical Vectors Logical vectors are commonly used for indexing > x <- c(1, 1, 2, 2) > x[x > 1] [1] 2 2 The logical binary operators are >, <, ==, <=, >= and !=, whilst ! is a unary operator. Some functions also return logical vectors, e.g. duplicated: > duplicated(x) [1] FALSE TRUE FALSE TRUE > !duplicated(x) [1] TRUE FALSE TRUE FALSE Single element logical vectors (scalars) are useful for flow control, see later. 8 / 80
  • 5. © 2012 Heather Turner Numeric Vectors The are several convenience function for creating numeric vectors, the most common being seq and rep. As they are so useful there are fast shortcuts for particular cases > x <- 3:5 > seq_len(3) [1] 1 2 3 > seq_along(x) [1] 1 2 3 > rep.int(1:2, times = c(2, 3)) [1] 1 1 2 2 2 9 / 80
  • 6. © 2012 Heather Turner Character Vectors Character vectors may be used for creating names > names(x) <- paste(LETTERS[1:3], 1229:1231, sep = "") > x A1229 B1230 C1231 3 4 5 Useful functions to extract information from character vectors include nchar, substr, strsplit, grep and gsub. The last two are for pattern matching and replacement > grep("123", names(x), fixed = TRUE) [1] 2 3 > as.numeric(gsub("[A-C]", "", names(x))) [1] 1229 1230 1231 The latter uses a regular expression, see references. 10 / 80
  • 7. © 2012 Heather Turner Factors Factors are a special type of vector representing a categorical variable as a vector of numeric codes with an attribute giving the levels > colour <- factor(rep(c("red", "green", "blue"), 2)) > str(colour) Factor w/ 3 levels "blue","green",..: 3 2 1 3 2 1 The function gl is useful for creating factors > gl(3, 1, length = 6, labels = c("blue", "green", "red")) [1] blue green red blue green red Levels: blue green red > gl(3, 2, length = 6, labels = c("red", "green", "blue")) [1] red red green green blue blue Levels: red green blue 11 / 80