More Data Structures

36-350
27 August 2014

Agenda

  • Arrays
  • Matrices
  • Lists
  • Dataframes
  • Structures of structures

Vector structures, starting with arrays

Many data structures in R are made by adding bells and whistles to vectors, so “vector structures”

Most useful: arrays

x <- c(7, 8, 10, 45)
x.arr <- array(x,dim=c(2,2))
x.arr
     [,1] [,2]
[1,]    7   10
[2,]    8   45

dim says how many rows and columns; filled by columns

Can have 3,4,n dimensional arrays; dim is a length-n vector

Some properties of the array:

dim(x.arr)
[1] 2 2
is.vector(x.arr)
[1] FALSE
is.array(x.arr)
[1] TRUE

Example: Eigenstuff

eigen() finds eigenvalues and eigenvectors of a matrix
Returns a list of a vector (the eigenvalues) and a matrix (the eigenvectors)

eigen(factory)
$values
[1] 41.556  1.444

$vectors
        [,1]    [,2]
[1,] 0.99966 -0.8413
[2,] 0.02593  0.5406
class(eigen(factory))
[1] "list"

With complicated objects, you can access parts of parts (of parts…)

factory %*% eigen(factory)$vectors[,2]
         [,1]
labor -1.2147
steel  0.7805
eigen(factory)$values[2] * eigen(factory)$vectors[,2]
[1] -1.2147  0.7805
eigen(factory)$values[2]
[1] 1.444
eigen(factory)[[1]][[2]] # NOT [[1,2]]
[1] 1.444

Summary

  • Arrays add multi-dimensional structure to vectors
  • Matrices act like you'd hope they would
  • Lists let us combine different types of data
  • Dataframes are hybrids of matrices and lists, for classic tabular data
  • Recursion lets us build complicated data structures out of the simpler ones