3. Data Frames
- Create a data frame.
df <- data.frame(
Name = c("Cat", "Dog", "Cow", "Pig"),
HowMany = c(5, 10, 15, 20),
IsPet = c(TRUE, TRUE, FALSE, FALSE))
- Print a data frame.
print(df)
## Name HowMany IsPet
## 1 Cat 5 TRUE
## 2 Dog 10 TRUE
## 3 Cow 15 FALSE
## 4 Pig 20 FALSE
- Index the data frame by row 1 and column 2.
df[1, 2]
## [1] 5
- Index the data frame by column name (shorthand).
df$HowMany
## [1] 5 10 15 20
- Subset rows using the equality operator.
df[df$IsPet == TRUE, ]
## Name HowMany IsPet
## 1 Cat 5 TRUE
## 2 Dog 10 TRUE