2.4 Exercises
Exercise 2.1 Practice referring to non-syntactic names in the following data frame
annoying <- tibble(
`1` = 1:10,
`2` = `1` * 2 + rnorm(length(`1`))
)
# extracting the variable called 1
annoying$`1`
#> [1] 1 2 3 4 5 6 7 8 9 10
# Plotting a scatterplot of `1` vs `2`
ggplot(annoying,mapping = aes(x = `1`, y = `2`)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE)
# Creating a new column called 3 which is `2` divided by `1`
mutate(annoying,`3`= `2`/`1`)
#> # A tibble: 10 x 3
#> `1` `2` `3`
#> <int> <dbl> <dbl>
#> 1 1 2.17 2.17
#> 2 2 3.85 1.93
#> 3 3 5.58 1.86
#> 4 4 7.20 1.80
#> 5 5 9.12 1.82
#> 6 6 12.1 2.02
#> # ... with 4 more rows
# Renaming the columns to `one`, `two` and `three`
(annoying <- rename(annoying,one = `1`, two = `2`))
#> # A tibble: 10 x 2
#> one two
#> <int> <dbl>
#> 1 1 2.17
#> 2 2 3.85
#> 3 3 5.58
#> 4 4 7.20
#> 5 5 9.12
#> 6 6 12.1
#> # ... with 4 more rows