SlideShare una empresa de Scribd logo
1 de 21
Descargar para leer sin conexión
Hadley Wickham
Stat405Data structures
Thursday, 11 November 2010
Assessment
• Final: there is no final
• Two groups still haven’t sent me
electronic versions of their project
• Many of you STILL HAVEN’T returned
your team evaluations
Thursday, 11 November 2010
1. Basic data types
2. Vectors, matrices & arrays
3. Lists & data.frames
Thursday, 11 November 2010
Vector
Matrix
Array
List
Data frame
1d
2d
nd
Same types Different types
Thursday, 11 November 2010
character
numeric
logical
mode()
length() A scalar is a vector of length 1
as.character(c(T, F))
as.character(seq_len(5))
as.logical(c(0, 1, 100))
as.logical(c("T", "F", "a"))
as.numeric(c("A", "100"))
as.numeric(c(T, F))
When vectors of
different types occur
in an expression,
they will be
automatically
coerced to the same
type: character >
numeric > logical
names() Optional, but useful
Technically, these are all atomic vectors
Thursday, 11 November 2010
Your turn
Experiment with automatic coercion.
What is happening in the following cases?
104 & 2 < 4
mean(diamonds$cut == "Good")
c(T, F, T, T, "F")
c(1, 2, 3, 4, F)
Thursday, 11 November 2010
Matrix (2d)
Array (>2d)
a <- seq_len(12)
dim(a) <- c(1, 12)
dim(a) <- c(4, 3)
dim(a) <- c(2, 6)
dim(a) <- c(3, 2, 2)
1 5 9
2 6 10
3 7 11
4 8 12
1 2 3 4 5 6 7 8 9 10 11 12
1 3 5 7 9 11
2 4 6 8 10 12
(1, 12)
(4, 3) (2, 6)Just like a vector.
Has mode() and
length().
Create with matrix
() or array(), or
from a vector by
setting dim()
as.vector()
converts back to a
vector
Thursday, 11 November 2010
List
Is also a vector (so has
mode, length and names),
but is different in that it can
store any other vector inside
it (including lists).
Use unlist() to convert to
a vector. Use as.list() to
convert a vector to a list.
c(1, 2, c(3, 4))
list(1, 2, list(3, 4))
c("a", T, 1:3)
list("a", T, 1:3)
a <- list(1:3, 1:5)
unlist(a)
as.list(a)
b <- list(1:3, "a", "b")
unlist(b)
Technically a recursive vector
Thursday, 11 November 2010
Data frame
List of vectors, each of the
same length. (Cross
between list and matrix)
Different to matrix in that
each column can have a
different type
Thursday, 11 November 2010
# How do you convert a matrix to a data frame?
# How do you convert a data frame to a matrix?
# What is different?
# What does these subsetting operations do?
# Why do they work? (Remember to use str)
diamonds[1]
diamonds[[1]]
diamonds[["cut"]]
Thursday, 11 November 2010
x <- sample(12)
# What's the difference between a & b?
a <- matrix(x, 4, 3)
b <- array(x, c(4, 3))
# What's the difference between x & y
y <- matrix(x, 12)
# How are these subsetting operations different?
a[, 1]
a[, 1, drop = FALSE]
a[1, ]
a[1, , drop = FALSE]
Thursday, 11 November 2010
1d names() length() c()
2d
colnames()
rownames()
ncol()
nrow()
cbind()
rbind()
nd dimnames() dim() abind()
(special package)
Thursday, 11 November 2010
b <- seq_len(10)
a <- letters[b]
# What sort of matrix does this create?
rbind(a, b)
cbind(a, b)
# Why would you want to use a data frame here?
# How would you create it?
Thursday, 11 November 2010
load(url("http://had.co.nz/stat405/data/quiz.rdata"))
# What is a? What is b?
# How are they different? How are they similar?
# How can you turn a in to b?
# How can you turn b in to a?
# What are c, d, and e?
# How are they different? How are they similar?
# How can you turn one into another?
# What is f?
# How can you extract the first element?
# How can you extract the first value in the first
# element?
Thursday, 11 November 2010
# a is numeric vector, containing the numbers 1 to 10
# b is a list of numeric scalars
# they contain the same values, but in a different format
identical(a[1], b[[1]])
identical(a, unlist(b))
identical(b, as.list(a))
# c is a named list
# d is a data.frame
# e is a numeric matrix
# From most to least general: c, d, e
identical(c, as.list(d))
identical(d, as.data.frame(c))
identical(e, data.matrix(d))
Thursday, 11 November 2010
# f is a list of matrices of different dimensions
f[[1]]
f[[1]][1, 2]
Thursday, 11 November 2010
# What does these subsetting operations do?
# Why do they work? (Remember to use str)
diamonds[1]
diamonds[[1]]
diamonds[["cut"]]
diamonds[["cut"]][1:10]
diamonds$cut[1:10]
Thursday, 11 November 2010
Vectors x[1:4] —
Matrices
Arrays
x[1:4, ]
x[, 2:3, ]
x[1:4, ,
drop = F]
Lists
x[[1]]
x$name
x[1]
Thursday, 11 November 2010
# What's the difference between a & b?
a <- matrix(x, 4, 3)
b <- array(x, c(4, 3))
# What's the difference between x & y
y <- matrix(x, 12)
# How are these subsetting operations different?
a[, 1]
a[, 1, drop = FALSE]
a[1, ]
a[1, , drop = FALSE]
Thursday, 11 November 2010
1d c()
2d
matrix()
data.frame()
t()
nd array() aperm()
Thursday, 11 November 2010
b <- seq_len(10)
a <- letters[b]
# What sort of matrix does this create?
rbind(a, b)
cbind(a, b)
# Why would you want to use a data frame here?
# How would you create it?
Thursday, 11 November 2010

Más contenido relacionado

La actualidad más candente

Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programmingizahn
 
Data Modeling, Normalization and Denormalization | Nordic PGDay 2018 | Dimitr...
Data Modeling, Normalization and Denormalization | Nordic PGDay 2018 | Dimitr...Data Modeling, Normalization and Denormalization | Nordic PGDay 2018 | Dimitr...
Data Modeling, Normalization and Denormalization | Nordic PGDay 2018 | Dimitr...Citus Data
 
Databases for Beginners SQLite
Databases for Beginners SQLiteDatabases for Beginners SQLite
Databases for Beginners SQLiteChristopher Wimble
 
SQL with PostgreSQL - Getting Started
SQL with PostgreSQL - Getting StartedSQL with PostgreSQL - Getting Started
SQL with PostgreSQL - Getting StartedOr Chen
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandasPiyush rai
 

La actualidad más candente (9)

Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
 
Data Modeling, Normalization and Denormalization | Nordic PGDay 2018 | Dimitr...
Data Modeling, Normalization and Denormalization | Nordic PGDay 2018 | Dimitr...Data Modeling, Normalization and Denormalization | Nordic PGDay 2018 | Dimitr...
Data Modeling, Normalization and Denormalization | Nordic PGDay 2018 | Dimitr...
 
D bpedia
D bpediaD bpedia
D bpedia
 
Databases for Beginners SQLite
Databases for Beginners SQLiteDatabases for Beginners SQLite
Databases for Beginners SQLite
 
SQL with PostgreSQL - Getting Started
SQL with PostgreSQL - Getting StartedSQL with PostgreSQL - Getting Started
SQL with PostgreSQL - Getting Started
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
 
1 구조체
1 구조체1 구조체
1 구조체
 
R Introduction
R IntroductionR Introduction
R Introduction
 
What is data structure
What is data structureWhat is data structure
What is data structure
 

Destacado

Destacado (20)

02 Ddply
02 Ddply02 Ddply
02 Ddply
 
27 development
27 development27 development
27 development
 
03 Modelling
03 Modelling03 Modelling
03 Modelling
 
01 Intro
01 Intro01 Intro
01 Intro
 
Graphical inference
Graphical inferenceGraphical inference
Graphical inference
 
Reshaping Data in R
Reshaping Data in RReshaping Data in R
Reshaping Data in R
 
27 development
27 development27 development
27 development
 
Plyr, one data analytic strategy
Plyr, one data analytic strategyPlyr, one data analytic strategy
Plyr, one data analytic strategy
 
Correlations, Trends, and Outliers in ggplot2
Correlations, Trends, and Outliers in ggplot2Correlations, Trends, and Outliers in ggplot2
Correlations, Trends, and Outliers in ggplot2
 
04 Wrapup
04 Wrapup04 Wrapup
04 Wrapup
 
24 modelling
24 modelling24 modelling
24 modelling
 
16 Sequences
16 Sequences16 Sequences
16 Sequences
 
20 date-times
20 date-times20 date-times
20 date-times
 
Model Visualisation (with ggplot2)
Model Visualisation (with ggplot2)Model Visualisation (with ggplot2)
Model Visualisation (with ggplot2)
 
03 Conditional
03 Conditional03 Conditional
03 Conditional
 
R workshop iii -- 3 hours to learn ggplot2 series
R workshop iii -- 3 hours to learn ggplot2 seriesR workshop iii -- 3 hours to learn ggplot2 series
R workshop iii -- 3 hours to learn ggplot2 series
 
Dplyr and Plyr
Dplyr and PlyrDplyr and Plyr
Dplyr and Plyr
 
Machine learning in R
Machine learning in RMachine learning in R
Machine learning in R
 
4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply Function4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply Function
 
Data manipulation with dplyr
Data manipulation with dplyrData manipulation with dplyr
Data manipulation with dplyr
 

Similar a 23 data-structures (20)

11 Data Structures
11 Data Structures11 Data Structures
11 Data Structures
 
R language introduction
R language introductionR language introduction
R language introduction
 
R_CheatSheet.pdf
R_CheatSheet.pdfR_CheatSheet.pdf
R_CheatSheet.pdf
 
R language, an introduction
R language, an introductionR language, an introduction
R language, an introduction
 
R교육1
R교육1R교육1
R교육1
 
R training2
R training2R training2
R training2
 
CS8391 Data Structures 2 mark Questions - Anna University Questions
CS8391 Data Structures 2 mark Questions - Anna University QuestionsCS8391 Data Structures 2 mark Questions - Anna University Questions
CS8391 Data Structures 2 mark Questions - Anna University Questions
 
R programming Fundamentals
R programming  FundamentalsR programming  Fundamentals
R programming Fundamentals
 
3. R- list and data frame
3. R- list and data frame3. R- list and data frame
3. R- list and data frame
 
1st lecture.ppt
1st lecture.ppt1st lecture.ppt
1st lecture.ppt
 
R programming by ganesh kavhar
R programming by ganesh kavharR programming by ganesh kavhar
R programming by ganesh kavhar
 
R-programming-training-in-mumbai
R-programming-training-in-mumbaiR-programming-training-in-mumbai
R-programming-training-in-mumbai
 
Language R
Language RLanguage R
Language R
 
R learning by examples
R learning by examplesR learning by examples
R learning by examples
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
 
tidyr.pdf
tidyr.pdftidyr.pdf
tidyr.pdf
 
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
 
Token
TokenToken
Token
 
DATA STRUCTURE BY SIVASANKARI
DATA STRUCTURE BY SIVASANKARIDATA STRUCTURE BY SIVASANKARI
DATA STRUCTURE BY SIVASANKARI
 

Más de Hadley Wickham (20)

19 tables
19 tables19 tables
19 tables
 
18 cleaning
18 cleaning18 cleaning
18 cleaning
 
17 polishing
17 polishing17 polishing
17 polishing
 
16 critique
16 critique16 critique
16 critique
 
15 time-space
15 time-space15 time-space
15 time-space
 
14 case-study
14 case-study14 case-study
14 case-study
 
13 case-study
13 case-study13 case-study
13 case-study
 
12 adv-manip
12 adv-manip12 adv-manip
12 adv-manip
 
11 adv-manip
11 adv-manip11 adv-manip
11 adv-manip
 
11 adv-manip
11 adv-manip11 adv-manip
11 adv-manip
 
10 simulation
10 simulation10 simulation
10 simulation
 
10 simulation
10 simulation10 simulation
10 simulation
 
09 bootstrapping
09 bootstrapping09 bootstrapping
09 bootstrapping
 
08 functions
08 functions08 functions
08 functions
 
07 problem-solving
07 problem-solving07 problem-solving
07 problem-solving
 
06 data
06 data06 data
06 data
 
05 subsetting
05 subsetting05 subsetting
05 subsetting
 
04 reports
04 reports04 reports
04 reports
 
03 extensions
03 extensions03 extensions
03 extensions
 
02 large
02 large02 large
02 large
 

Último

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 

Último (20)

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

23 data-structures

  • 2. Assessment • Final: there is no final • Two groups still haven’t sent me electronic versions of their project • Many of you STILL HAVEN’T returned your team evaluations Thursday, 11 November 2010
  • 3. 1. Basic data types 2. Vectors, matrices & arrays 3. Lists & data.frames Thursday, 11 November 2010
  • 4. Vector Matrix Array List Data frame 1d 2d nd Same types Different types Thursday, 11 November 2010
  • 5. character numeric logical mode() length() A scalar is a vector of length 1 as.character(c(T, F)) as.character(seq_len(5)) as.logical(c(0, 1, 100)) as.logical(c("T", "F", "a")) as.numeric(c("A", "100")) as.numeric(c(T, F)) When vectors of different types occur in an expression, they will be automatically coerced to the same type: character > numeric > logical names() Optional, but useful Technically, these are all atomic vectors Thursday, 11 November 2010
  • 6. Your turn Experiment with automatic coercion. What is happening in the following cases? 104 & 2 < 4 mean(diamonds$cut == "Good") c(T, F, T, T, "F") c(1, 2, 3, 4, F) Thursday, 11 November 2010
  • 7. Matrix (2d) Array (>2d) a <- seq_len(12) dim(a) <- c(1, 12) dim(a) <- c(4, 3) dim(a) <- c(2, 6) dim(a) <- c(3, 2, 2) 1 5 9 2 6 10 3 7 11 4 8 12 1 2 3 4 5 6 7 8 9 10 11 12 1 3 5 7 9 11 2 4 6 8 10 12 (1, 12) (4, 3) (2, 6)Just like a vector. Has mode() and length(). Create with matrix () or array(), or from a vector by setting dim() as.vector() converts back to a vector Thursday, 11 November 2010
  • 8. List Is also a vector (so has mode, length and names), but is different in that it can store any other vector inside it (including lists). Use unlist() to convert to a vector. Use as.list() to convert a vector to a list. c(1, 2, c(3, 4)) list(1, 2, list(3, 4)) c("a", T, 1:3) list("a", T, 1:3) a <- list(1:3, 1:5) unlist(a) as.list(a) b <- list(1:3, "a", "b") unlist(b) Technically a recursive vector Thursday, 11 November 2010
  • 9. Data frame List of vectors, each of the same length. (Cross between list and matrix) Different to matrix in that each column can have a different type Thursday, 11 November 2010
  • 10. # How do you convert a matrix to a data frame? # How do you convert a data frame to a matrix? # What is different? # What does these subsetting operations do? # Why do they work? (Remember to use str) diamonds[1] diamonds[[1]] diamonds[["cut"]] Thursday, 11 November 2010
  • 11. x <- sample(12) # What's the difference between a & b? a <- matrix(x, 4, 3) b <- array(x, c(4, 3)) # What's the difference between x & y y <- matrix(x, 12) # How are these subsetting operations different? a[, 1] a[, 1, drop = FALSE] a[1, ] a[1, , drop = FALSE] Thursday, 11 November 2010
  • 12. 1d names() length() c() 2d colnames() rownames() ncol() nrow() cbind() rbind() nd dimnames() dim() abind() (special package) Thursday, 11 November 2010
  • 13. b <- seq_len(10) a <- letters[b] # What sort of matrix does this create? rbind(a, b) cbind(a, b) # Why would you want to use a data frame here? # How would you create it? Thursday, 11 November 2010
  • 14. load(url("http://had.co.nz/stat405/data/quiz.rdata")) # What is a? What is b? # How are they different? How are they similar? # How can you turn a in to b? # How can you turn b in to a? # What are c, d, and e? # How are they different? How are they similar? # How can you turn one into another? # What is f? # How can you extract the first element? # How can you extract the first value in the first # element? Thursday, 11 November 2010
  • 15. # a is numeric vector, containing the numbers 1 to 10 # b is a list of numeric scalars # they contain the same values, but in a different format identical(a[1], b[[1]]) identical(a, unlist(b)) identical(b, as.list(a)) # c is a named list # d is a data.frame # e is a numeric matrix # From most to least general: c, d, e identical(c, as.list(d)) identical(d, as.data.frame(c)) identical(e, data.matrix(d)) Thursday, 11 November 2010
  • 16. # f is a list of matrices of different dimensions f[[1]] f[[1]][1, 2] Thursday, 11 November 2010
  • 17. # What does these subsetting operations do? # Why do they work? (Remember to use str) diamonds[1] diamonds[[1]] diamonds[["cut"]] diamonds[["cut"]][1:10] diamonds$cut[1:10] Thursday, 11 November 2010
  • 18. Vectors x[1:4] — Matrices Arrays x[1:4, ] x[, 2:3, ] x[1:4, , drop = F] Lists x[[1]] x$name x[1] Thursday, 11 November 2010
  • 19. # What's the difference between a & b? a <- matrix(x, 4, 3) b <- array(x, c(4, 3)) # What's the difference between x & y y <- matrix(x, 12) # How are these subsetting operations different? a[, 1] a[, 1, drop = FALSE] a[1, ] a[1, , drop = FALSE] Thursday, 11 November 2010
  • 20. 1d c() 2d matrix() data.frame() t() nd array() aperm() Thursday, 11 November 2010
  • 21. b <- seq_len(10) a <- letters[b] # What sort of matrix does this create? rbind(a, b) cbind(a, b) # Why would you want to use a data frame here? # How would you create it? Thursday, 11 November 2010