Matrices and Data Frames
age sex size count census
Matrices and Data Frames
Matrix Data Frame
Matrices and Data Frames
Matrix Data Frame
Used as your primary data
object. Essentially a spreadsheet.
Matrices and Data Frames
Matrix Data Frame
Used frequently in mathematical Used as your primary data
applications, models object. Essentially a spreadsheet.
More computationally efficient
Matrices Must be a multiple of your data length
To create a matrix( ):
• Requires data
• Number of rows or columns
Matrices Must be a multiple of your data length
To create a matrix( ):
• Requires data
• Number of rows or columns
Want to put values by rows instead of
columns?
• byrow = TRUE
Matrices
Labeling your rows and columns
colnames( )
Matrices
Labeling your rows and columns
colnames( )
rownames( )
Matrices
Piece together a matrix or add to one:
• rbind( ) and cbind( )
Matrices
Piece together a matrix or add to one:
• rbind( ) and cbind( )
Data Frames
Like a matrix, but can have any class of data in a given column
- Because each column is essentially a vector, the class of data must be
consistent in each column
Data Frames
Create a [Link]( )
• Provide objects to turn into columns
Data Frames
Most functions that work with matrices work with data frames
• rownames, colnames, rbind, cbind etc…
Use dim( ) to get dimensions, and str( ) to summarize your data frame
Data Frames
Most functions that work with matrices work with data frames
• rownames, colnames, rbind, cbind etc…
Use dim( ) to get dimensions, and str( ) to summarize your data frame
Row, Column
Data Frames
Most functions that work with matrices work with data frames
• rownames, colnames, rbind, cbind etc…
Use dim( ) to get dimensions, and str( ) to summarize your data frame
Column name : data class
Subsetting 2-Dimensional Objects
,1 ,2 ,3 ,4 ,5
$age $sex $size $count $census
1,
2,
3,
4,
Indices
object[row,col]
Indices
object[row,col]
Indices
object[row,col]
Indices
object[row,col]
single value, vector, or nothing
Indices
Indices
Indices
Indices
Indices
$ Operator
Data frames can be subset the same way as matrices
We also have a special way to subset data frames
• The $ operator
$ Operator
Data frames can be subset the same way as matrices
We also have a special way to subset data frames
• The $ operator
$ Operator
$ Operator
$ Operator
Logic $ operator is useful for logical subsets
Logic $ operator is useful for logical subsets
Logic $ operator is useful for logical subsets
Logic $ operator is useful for logical subsets
Logic $ operator is useful for logical subsets
Applications?
Comparison of height between yes and no responses
• T-Test
Logic
Comparison of height between yes and no responses
• T-Test
Logic
Comparison of height between yes and no responses
• T-Test