0% found this document useful (0 votes)
4 views38 pages

Matrices Dataframes

The document discusses matrices and data frames, highlighting their differences and uses in data analysis. Matrices are primarily used for mathematical applications and require consistent data types, while data frames can hold different data types in each column and are treated like spreadsheets. It also covers how to create, label, and subset these data structures, including the use of the $ operator for data frames.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views38 pages

Matrices Dataframes

The document discusses matrices and data frames, highlighting their differences and uses in data analysis. Matrices are primarily used for mathematical applications and require consistent data types, while data frames can hold different data types in each column and are treated like spreadsheets. It also covers how to create, label, and subset these data structures, including the use of the $ operator for data frames.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like