Recap of objects & classes
More sophisticated objects
Objects are the nouns of programming languages
They have names and they store something
Objects can be of a different classes. What type of information is stored in the object? Some of the options are:
Objects can be of a different classes. What type of information is stored in the object? Some of the options are:
Objects can be of a different classes. What type of information is stored in the object? Some of the options are:
Objects can be of a different classes. What type of information is stored in the object? Some of the options are:
Objects can be of a different classes. What type of information is stored in the object? Some of the options are:
TRUE
or FALSE
T
/F
...True
/False
/t
/f
Objects can be of a different classes. What type of information is stored in the object? Some of the options are:
TRUE
or FALSE
T
/F
...True
/False
/t
/f
NA
Another object class is a data.frame. You can think of this as an Excel sheet. empire
is an example of a data.frame. When you view it in R
, it looks like this:
Typically, this is what we want our data to look like. In empire
, we have 6 column vectors. But they are NOT stored as 6 separate objects -- they are combined because they are all related to one another.
Typically, this is what we want our data to look like. In empire
, we have 6 column vectors. But they are NOT stored as 6 separate objects -- they are combined because they are all related to one another.
Data.frames are 2-dimensional
Typically, this is what we want our data to look like. In empire
, we have 6 column vectors. But they are NOT stored as 6 separate objects -- they are combined because they are all related to one another.
Data.frames are 2-dimensional
Every row has 6 pieces of data that are associated with one another...
Every column has 10 observations...
Data.frames can be indexed just like vectors.
Data.frames can be indexed just like vectors.
Except: Data.frames have 2 dimensions!
data.frame[rows, columns]
data.frame[rows, columns]
What should we get if we typed empire[1:6,5]
?
data.frame[rows, columns]
What should we get if we typed empire[1:6,5]
?
data.frame[rows, columns]
What do we get if we type empire[1:6,5]
?
empire[1:6,5]
## [1] "Tatooine" "Tatooine" "Naboo" "Tatooine" "Alderaan" "Stewjon"
If you want all of something, leave it blank.
If you want all of something, leave it blank.
All the rows of column 2
empire[,2]
## [1] 172 167 96 202 150 182 228 180 66 183
If you want all of something, leave it blank.
All the rows of column 2
empire[,2]
## [1] 172 167 96 202 150 182 228 180 66 183
All the columns of row 5
empire[5,]
## name height mass sex homeworld species## 5 Leia Organa 150 49 female Alderaan Human
Sometimes it's easy enough to remember the row index or column index that you want. But often, we forget!
One of the benefits of a data.frame is that you can access a column by using the column name.
data.frame$column.name
empire$height
## [1] 172 167 96 202 150 182 228 180 66 183
empire$height
## [1] 172 167 96 202 150 182 228 180 66 183
# Make a matrixtestMatrix <- matrix(data = 1:12, nrow = 4, ncol = 3)testMatrix
## [,1] [,2] [,3]## [1,] 1 5 9## [2,] 2 6 10## [3,] 3 7 11## [4,] 4 8 12
# Try to access "V1"testMatrix$V1
## Error in testMatrix$V1: $ operator is invalid for atomic vectors
# Convert to data.frametestDataFrame <- as.data.frame(testMatrix)testDataFrame
## V1 V2 V3## 1 1 5 9## 2 2 6 10## 3 3 7 11## 4 4 8 12
# Now try to access column 2 using the "V2" headingtestDataFrame$V2
## [1] 5 6 7 8
tidyverse
class()
on a tibble, it might show up as tbl_df
empire
)exampleList <- list("hello", empire[1:3,], c(2:12))# To get the first element (the word "hello")exampleList[1]
## [[1]]## [1] "hello"
empire
)exampleList <- list("hello", empire[1:3,], c(2:12))# To get the second element (the `empire` data.frame)exampleList[2]
## [[1]]## name height mass sex homeworld species## 1 Luke Skywalker 172 77 male Tatooine Human## 2 C-3PO 167 75 none Tatooine Droid## 3 R2-D2 96 32 none Naboo Droid
empire
)exampleList <- list("hello", empire[1:3,], c(2:12))# To get the third element (the vector of numbers 2 through 12)exampleList[3]
## [[1]]## [1] 2 3 4 5 6 7 8 9 10 11 12
[[ ]]
# Single bracketsexampleList[2]
## [[1]]## name height mass sex homeworld species## 1 Luke Skywalker 172 77 male Tatooine Human## 2 C-3PO 167 75 none Tatooine Droid## 3 R2-D2 96 32 none Naboo Droid
# Single brackets doesn't get you "in"exampleList[2]$mass
## NULL
[[ ]]
# Single bracketsexampleList[2]
## [[1]]## name height mass sex homeworld species## 1 Luke Skywalker 172 77 male Tatooine Human## 2 C-3PO 167 75 none Tatooine Droid## 3 R2-D2 96 32 none Naboo Droid
# Double bracketsexampleList[[2]]
## name height mass sex homeworld species## 1 Luke Skywalker 172 77 male Tatooine Human## 2 C-3PO 167 75 none Tatooine Droid## 3 R2-D2 96 32 none Naboo Droid
[[ ]]
# Double bracketsexampleList[[2]]
## name height mass sex homeworld species## 1 Luke Skywalker 172 77 male Tatooine Human## 2 C-3PO 167 75 none Tatooine Droid## 3 R2-D2 96 32 none Naboo Droid
# Double brackets gets you "in"exampleList[[2]]$mass
## [1] 77 75 32
Recap of objects & classes
More sophisticated objects
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |