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(dplyr)
library(tidyverse)
mpg
, rename the column
year
to YEAR
using rename
.mpg <- mpg %>% rename(YEAR = year)
mpg
to all upper case. Use
rename_with
, and the toupper
command (or
colnames
). Save the output to Upper_mpg
.Upper_mpg <- rename_with(mpg, toupper)
head(Upper_mpg)
## # A tibble: 6 × 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(l5) f 18 29 p compa…
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
mpg
by cyl
in
increasing order. Use arrange()
. Reassign the
mpg
data!mpg <- mpg %>% arrange(cyl)
mpg
called cty2
,
which is equal to cty^2
, using mutate()
.mpg <- mpg %>% mutate(cty2 = cty^2)
head(mpg)
## # A tibble: 6 × 12
## manufacturer model displ YEAR cyl trans drv cty hwy fl class cty2
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr> <dbl>
## 1 audi a4 1.8 1999 4 auto… f 18 29 p comp… 324
## 2 audi a4 1.8 1999 4 manu… f 21 29 p comp… 441
## 3 audi a4 2 2008 4 manu… f 20 31 p comp… 400
## 4 audi a4 2 2008 4 auto… f 21 30 p comp… 441
## 5 audi a4 q… 1.8 1999 4 manu… 4 18 26 p comp… 324
## 6 audi a4 q… 1.8 1999 4 auto… 4 16 25 p comp… 256
mpg
called displ
to
be 10 times the current value using mutate()
.mpg <- mpg %>% mutate(displ = displ*10)
head(mpg)
## # A tibble: 6 × 12
## manufacturer model displ YEAR cyl trans drv cty hwy fl class cty2
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr> <dbl>
## 1 audi a4 18 1999 4 auto… f 18 29 p comp… 324
## 2 audi a4 18 1999 4 manu… f 21 29 p comp… 441
## 3 audi a4 20 2008 4 manu… f 20 31 p comp… 400
## 4 audi a4 20 2008 4 auto… f 21 30 p comp… 441
## 5 audi a4 q… 18 1999 4 manu… 4 18 26 p comp… 324
## 6 audi a4 q… 18 1999 4 auto… 4 16 25 p comp… 256