SlideShare une entreprise Scribd logo
1  sur  21
Télécharger pour lire hors ligne
Stat405                Data structures


                                Hadley Wickham
Wednesday, 30 September 2009
1. Outline for next couple of weeks
               2. Basic data types
               3. Vectors, matrices & arrays
               4. Lists & data.frames




Wednesday, 30 September 2009
Outline
                   More details about the underlying data
                   structures we’ve been using so far.
                   Then pulling apart those data structures
                   (particularly data frames), operating on
                   each piece and putting them back
                   together again.
                   An alternative to for loops when each
                   piece is independent.


Wednesday, 30 September 2009
1d    Vector          List



                           2d   Matrix       Data frame




                           nd    Array


                                Same types   Different types


Wednesday, 30 September 2009
When vectors of
        character              as.character(c(T, F))
                                                              different types occur
                               as.character(seq_len(5))       in an expression,
                               as.logical(c(0, 1, 100))       they will be
        numeric                                               automatically
                               as.logical(c("T", "F", "a"))
                               as.numeric(c("A", "100"))      coerced to the same
        logical                as.numeric(c(T, F))
                                                              type: character >
                                                              numeric > logical

       mode()
       names()                 Optional, but useful
       length()                A scalar is a vector of length 1




Technically, these are all atomic vectors
Wednesday, 30 September 2009
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)


Wednesday, 30 September 2009
Matrix (2d)               (1, 12)

     Array (>2d)                1    2   3        4   5   6       7       8       9 10 11 12

      Just like a vector.                (4, 3)           (2, 6)
      Has mode() and                      1 5 9               1       3       5     7   9 11
      length().
                                          2 6 10              2       4       6     8 10 12
      Create with matrix                  3 7 11
      () or array(), or                                       a <- seq_len(12)
                                          4 8 12
      from a vector by                                        dim(a) <- c(1, 12)
      setting dim()                                           dim(a) <- c(4, 3)
                                                              dim(a) <- c(2, 6)
      as.vector()                                             dim(a) <- c(3, 2, 2)
      converts back to a
      vector
Wednesday, 30 September 2009
List
       Is also a vector (so has          c(1, 2, c(3, 4))
       mode, length and names),          list(1, 2, list(3, 4))
       but is different in that it can
       store any other vector inside     c("a", T, 1:3)
       it (including lists).             list("a", T, 1:3)



       Use unlist() to convert to        a <- list(1:3, 1:5)
       a vector. Use as.list() to        unlist(a)
       convert a vector to a list.       as.list(a)


                                         b <- list(1:3, "a", "b")
                                         unlist(b)



Technically a recursive vector
Wednesday, 30 September 2009
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




Wednesday, 30 September 2009
# 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"]]




Wednesday, 30 September 2009
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]


Wednesday, 30 September 2009
1d                   names()     length()        c()


                               colnames()    ncol()      cbind()
           2d
                               rownames()    nrow()      rbind()


                                                         abind()
           nd                  dimnames()    dim()     (special package)




Wednesday, 30 September 2009
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?




Wednesday, 30 September 2009
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?

Wednesday, 30 September 2009
# 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))


Wednesday, 30 September 2009
# f is a list of matrices of different dimensions

     f[[1]]
     f[[1]][1, 2]




Wednesday, 30 September 2009
# 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]




Wednesday, 30 September 2009
Vectors      x[1:4]      —



                 Matrices x[1:4, ]        x[1:4, ,
                  Arrays x[, 2:3, ]      drop = F]

                                x[[1]]
                        Lists   x$name
                                           x[1]



Wednesday, 30 September 2009
# 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]



Wednesday, 30 September 2009
1d        c()


                                 matrix()
                         2d                     t()
                               data.frame()


                         nd      array()      aperm()



Wednesday, 30 September 2009
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?




Wednesday, 30 September 2009

Contenu connexe

Tendances

Tendances (20)

Application of Derivative 3
Application of Derivative 3Application of Derivative 3
Application of Derivative 3
 
Seminar psu 20.10.2013
Seminar psu 20.10.2013Seminar psu 20.10.2013
Seminar psu 20.10.2013
 
Seminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mmeSeminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mme
 
Application of Derivative 5
Application of Derivative 5Application of Derivative 5
Application of Derivative 5
 
Day 4b iteration and functions for-loops.pptx
Day 4b   iteration and functions  for-loops.pptxDay 4b   iteration and functions  for-loops.pptx
Day 4b iteration and functions for-loops.pptx
 
Numpy python cheat_sheet
Numpy python cheat_sheetNumpy python cheat_sheet
Numpy python cheat_sheet
 
NumPy Refresher
NumPy RefresherNumPy Refresher
NumPy Refresher
 
R gráfico
R gráficoR gráfico
R gráfico
 
Day 4a iteration and functions.pptx
Day 4a   iteration and functions.pptxDay 4a   iteration and functions.pptx
Day 4a iteration and functions.pptx
 
Hierarchical clustering techniques
Hierarchical clustering techniquesHierarchical clustering techniques
Hierarchical clustering techniques
 
Introduction to Haskell@Open Source Conference 2007 Hokkaido
Introduction to Haskell@Open Source Conference 2007 HokkaidoIntroduction to Haskell@Open Source Conference 2007 Hokkaido
Introduction to Haskell@Open Source Conference 2007 Hokkaido
 
Scientific Computing with Python Webinar March 19: 3D Visualization with Mayavi
Scientific Computing with Python Webinar March 19: 3D Visualization with MayaviScientific Computing with Python Webinar March 19: 3D Visualization with Mayavi
Scientific Computing with Python Webinar March 19: 3D Visualization with Mayavi
 
Introduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersIntroduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning Programmers
 
10. Getting Spatial
10. Getting Spatial10. Getting Spatial
10. Getting Spatial
 
gfg
gfggfg
gfg
 
Machine Learning with Go
Machine Learning with GoMachine Learning with Go
Machine Learning with Go
 
Lesson 27: Integration by Substitution (Section 041 slides)
Lesson 27: Integration by Substitution (Section 041 slides)Lesson 27: Integration by Substitution (Section 041 slides)
Lesson 27: Integration by Substitution (Section 041 slides)
 
Sharbani bhattacharya sacta 2014
Sharbani bhattacharya sacta 2014Sharbani bhattacharya sacta 2014
Sharbani bhattacharya sacta 2014
 
CS 354 Graphics Math
CS 354 Graphics MathCS 354 Graphics Math
CS 354 Graphics Math
 
19 tables
19 tables19 tables
19 tables
 

En vedette (7)

Application of hashing in better alg design tanmay
Application of hashing in better alg design tanmayApplication of hashing in better alg design tanmay
Application of hashing in better alg design tanmay
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
 
Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2
 
Ch17 Hashing
Ch17 HashingCh17 Hashing
Ch17 Hashing
 
Hash tables
Hash tablesHash tables
Hash tables
 
11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patil11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patil
 
Hashing and Hash Tables
Hashing and Hash TablesHashing and Hash Tables
Hashing and Hash Tables
 

Similaire à 11 Data Structures

R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptx
kalai75
 

Similaire à 11 Data Structures (20)

03 Cleaning
03 Cleaning03 Cleaning
03 Cleaning
 
23 data-structures
23 data-structures23 data-structures
23 data-structures
 
R
RR
R
 
R_CheatSheet.pdf
R_CheatSheet.pdfR_CheatSheet.pdf
R_CheatSheet.pdf
 
08 functions
08 functions08 functions
08 functions
 
tidyr.pdf
tidyr.pdftidyr.pdf
tidyr.pdf
 
08 Functions
08 Functions08 Functions
08 Functions
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environment
 
3 Data Structure in R
3 Data Structure in R3 Data Structure in R
3 Data Structure in R
 
Table of Useful R commands.
Table of Useful R commands.Table of Useful R commands.
Table of Useful R commands.
 
05 subsetting
05 subsetting05 subsetting
05 subsetting
 
R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptx
 
R Programming: Learn To Manipulate Strings In R
R Programming: Learn To Manipulate Strings In RR Programming: Learn To Manipulate Strings In R
R Programming: Learn To Manipulate Strings In R
 
R Programming Homework Help
R Programming Homework HelpR Programming Homework Help
R Programming Homework Help
 
The Very ^ 2 Basics of R
The Very ^ 2 Basics of RThe Very ^ 2 Basics of R
The Very ^ 2 Basics of R
 
R Basics
R BasicsR Basics
R Basics
 
R Cheat Sheet for Data Analysts and Statisticians.pdf
R Cheat Sheet for Data Analysts and Statisticians.pdfR Cheat Sheet for Data Analysts and Statisticians.pdf
R Cheat Sheet for Data Analysts and Statisticians.pdf
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
R language introduction
R language introductionR language introduction
R language introduction
 
matlab.docx
matlab.docxmatlab.docx
matlab.docx
 

Plus de Hadley Wickham (20)

27 development
27 development27 development
27 development
 
27 development
27 development27 development
27 development
 
24 modelling
24 modelling24 modelling
24 modelling
 
Graphical inference
Graphical inferenceGraphical inference
Graphical inference
 
R packages
R packagesR packages
R packages
 
22 spam
22 spam22 spam
22 spam
 
21 spam
21 spam21 spam
21 spam
 
20 date-times
20 date-times20 date-times
20 date-times
 
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
 

Dernier

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Dernier (20)

AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 

11 Data Structures

  • 1. Stat405 Data structures Hadley Wickham Wednesday, 30 September 2009
  • 2. 1. Outline for next couple of weeks 2. Basic data types 3. Vectors, matrices & arrays 4. Lists & data.frames Wednesday, 30 September 2009
  • 3. Outline More details about the underlying data structures we’ve been using so far. Then pulling apart those data structures (particularly data frames), operating on each piece and putting them back together again. An alternative to for loops when each piece is independent. Wednesday, 30 September 2009
  • 4. 1d Vector List 2d Matrix Data frame nd Array Same types Different types Wednesday, 30 September 2009
  • 5. When vectors of character as.character(c(T, F)) different types occur as.character(seq_len(5)) in an expression, as.logical(c(0, 1, 100)) they will be numeric automatically as.logical(c("T", "F", "a")) as.numeric(c("A", "100")) coerced to the same logical as.numeric(c(T, F)) type: character > numeric > logical mode() names() Optional, but useful length() A scalar is a vector of length 1 Technically, these are all atomic vectors Wednesday, 30 September 2009
  • 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) Wednesday, 30 September 2009
  • 7. Matrix (2d) (1, 12) Array (>2d) 1 2 3 4 5 6 7 8 9 10 11 12 Just like a vector. (4, 3) (2, 6) Has mode() and 1 5 9 1 3 5 7 9 11 length(). 2 6 10 2 4 6 8 10 12 Create with matrix 3 7 11 () or array(), or a <- seq_len(12) 4 8 12 from a vector by dim(a) <- c(1, 12) setting dim() dim(a) <- c(4, 3) dim(a) <- c(2, 6) as.vector() dim(a) <- c(3, 2, 2) converts back to a vector Wednesday, 30 September 2009
  • 8. List Is also a vector (so has c(1, 2, c(3, 4)) mode, length and names), list(1, 2, list(3, 4)) but is different in that it can store any other vector inside c("a", T, 1:3) it (including lists). list("a", T, 1:3) Use unlist() to convert to a <- list(1:3, 1:5) a vector. Use as.list() to unlist(a) convert a vector to a list. as.list(a) b <- list(1:3, "a", "b") unlist(b) Technically a recursive vector Wednesday, 30 September 2009
  • 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 Wednesday, 30 September 2009
  • 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"]] Wednesday, 30 September 2009
  • 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] Wednesday, 30 September 2009
  • 12. 1d names() length() c() colnames() ncol() cbind() 2d rownames() nrow() rbind() abind() nd dimnames() dim() (special package) Wednesday, 30 September 2009
  • 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? Wednesday, 30 September 2009
  • 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? Wednesday, 30 September 2009
  • 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)) Wednesday, 30 September 2009
  • 16. # f is a list of matrices of different dimensions f[[1]] f[[1]][1, 2] Wednesday, 30 September 2009
  • 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] Wednesday, 30 September 2009
  • 18. Vectors x[1:4] — Matrices x[1:4, ] x[1:4, , Arrays x[, 2:3, ] drop = F] x[[1]] Lists x$name x[1] Wednesday, 30 September 2009
  • 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] Wednesday, 30 September 2009
  • 20. 1d c() matrix() 2d t() data.frame() nd array() aperm() Wednesday, 30 September 2009
  • 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? Wednesday, 30 September 2009