In this lab you can use the interactive console to explore but please
record your commands here. Remember anything you type here can be “sent”
to the console with Cmd-Enter (OS-X) or Cntr-Enter (Windows/Linux) (But
only in side the {r}
areas).
library(tidyverse)
mpg
dataset [hint: it’s in
the ggplot2
package].mpg
## # A tibble: 234 × 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto… f 18 29 p comp…
## 2 audi a4 1.8 1999 4 manu… f 21 29 p comp…
## 3 audi a4 2 2008 4 manu… f 20 31 p comp…
## 4 audi a4 2 2008 4 auto… f 21 30 p comp…
## 5 audi a4 2.8 1999 6 auto… f 16 26 p comp…
## 6 audi a4 2.8 1999 6 manu… f 18 26 p comp…
## 7 audi a4 3.1 2008 6 auto… f 18 27 p comp…
## 8 audi a4 quattro 1.8 1999 4 manu… 4 18 26 p comp…
## 9 audi a4 quattro 1.8 1999 4 auto… 4 16 25 p comp…
## 10 audi a4 quattro 2 2008 4 manu… 4 20 28 p comp…
## # ℹ 224 more rows
mpg
?class(mpg)
## [1] "tbl_df" "tbl" "data.frame"
mpg
dataset?dim(mpg)
## [1] 234 11
nrow(mpg)
## [1] 234
ncol(mpg)
## [1] 11
glimpse(mpg)
## Rows: 234
## Columns: 11
## $ manufacturer <chr> "audi", "audi", "audi", "audi", "audi", "audi", "audi", "…
## $ model <chr> "a4", "a4", "a4", "a4", "a4", "a4", "a4", "a4 quattro", "…
## $ displ <dbl> 1.8, 1.8, 2.0, 2.0, 2.8, 2.8, 3.1, 1.8, 1.8, 2.0, 2.0, 2.…
## $ year <int> 1999, 1999, 2008, 2008, 1999, 1999, 2008, 1999, 1999, 200…
## $ cyl <int> 4, 4, 4, 4, 6, 6, 6, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 8, 8, …
## $ trans <chr> "auto(l5)", "manual(m5)", "manual(m6)", "auto(av)", "auto…
## $ drv <chr> "f", "f", "f", "f", "f", "f", "f", "4", "4", "4", "4", "4…
## $ cty <int> 18, 21, 20, 21, 16, 18, 18, 18, 16, 20, 19, 15, 17, 17, 1…
## $ hwy <int> 29, 29, 31, 30, 26, 26, 27, 26, 25, 28, 27, 25, 25, 25, 2…
## $ fl <chr> "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p…
## $ class <chr> "compact", "compact", "compact", "compact", "compact", "c…
manufacturer
, model
and
year
columns from the mpg
dataset.select(mpg, manufacturer, model, year)
## # A tibble: 234 × 3
## manufacturer model year
## <chr> <chr> <int>
## 1 audi a4 1999
## 2 audi a4 1999
## 3 audi a4 2008
## 4 audi a4 2008
## 5 audi a4 1999
## 6 audi a4 1999
## 7 audi a4 2008
## 8 audi a4 quattro 1999
## 9 audi a4 quattro 1999
## 10 audi a4 quattro 2008
## # ℹ 224 more rows
cty
) is greater than 20 and highway fuel economy
(hwy
) is greater than 30. Assign this output to an object
called eff
. How many cars/rows are present?eff <- mpg %>% filter(cty > 20, hwy > 30)
dim(eff)
## [1] 21 11
eff
dataset) were
manufactured in the year 1999?eff %>% filter(year == 1999) %>% nrow()
## [1] 9
mpg
dataset that do
not have 4 cylinder engines. How many cars/rows are
there?mpg %>% filter(cyl != 4) %>% nrow()
## [1] 153
# both give same result
mpg %>% filter(class %in% c("suv", "minivan")) %>% nrow()
## [1] 73
mpg %>% filter(class =="suv" | class == "minivan") %>% nrow()
## [1] 73
displ
) greater than 4
and that are all 4 wheel drive (drv
). How many cars/rows
are there.mpg %>% filter(displ > 4 & drv == "4") %>% nrow()
## [1] 49