Question 1
1.Install the “dplyr” and “tidyverse” packages.
2. Load the “dplyr” and “tidyverse” packages.
3. We will use the “mtcars” dataset for this exercise. Take a look at
the dataset with the “glimpse” command.
library(dplyr)
library(tidyverse)
glimpse(mtcars)
## Rows: 32
## Columns: 11
## $ mpg <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8,…
## $ cyl <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8,…
## $ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8, 16…
## $ hp <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180…
## $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92,…
## $ wt <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.…
## $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90, 18…
## $ vs <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,…
## $ am <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,…
## $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3,…
## $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2,…
4.Print out the hp column using the select() function. Try using the pull() function instead of select to see what the difference is.
#select()
mtcars %>% select(hp)
#pull()
mtcars %>% pull(hp)
## [1] 110 110 93 110 175 105 245 62 95 123 123 180 180 180 205 215 230 66 52
## [20] 65 97 150 150 245 175 66 91 113 264 175 335 109
The select() function will return a data
frame with just the horsepower (hp) column. We would use this
if we want to continue working within the data frame to add
transformations or to keep column names, etc.
Using the pull() function will extract the hp column in
the mtcars dataset as a vector. We might use this if we
needed to work with the raw values to compute the mean or for
graphing.
mtcars %>% select(-hp)
mtcars %>% select(mpg, hp, vs:gear)
mtcars %>% select(1,2,8:10)
mycars = mtcars %>% select(miles_per_gallon = mpg, horse_power = hp)
mycars <- mycars %>% mutate(km_per_litre = 0.425*miles_per_gallon)
mycars %>% filter(row.names(mycars) == "Lotus Europa")
Question 2. Create a lecture video where you explain
a topic in R that you find challenging by using the interactive
“swirl”-package.
We can enter a swirl lesson by typing swirl() and
ending a swirl lesson by typing bye() or using the esc
key.
Question 3.
I have completed through chapter16.