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

GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default  Presentation : Data Analysis Project PPTPredictive Analysis for Loan Default  Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPTBoston Institute of Analytics
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfSocial Samosa
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 
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
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDRafezzaman
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
Multiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfMultiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfchwongval
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Cantervoginip
 
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
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfJohn Sterrett
 
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
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 
Defining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data StoryDefining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data StoryJeremy Anderson
 
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
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home ServiceSapana Sha
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 

Recently uploaded (20)

GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default  Presentation : Data Analysis Project PPTPredictive Analysis for Loan Default  Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 
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
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
Multiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfMultiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdf
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Canter
 
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...
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdf
 
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
 
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
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 
Defining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data StoryDefining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data Story
 
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...
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 

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