forked from jrnold/r4ds-exercise-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tibble.Rmd
199 lines (142 loc) · 4.97 KB
/
tibble.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# Tibbles {#tibbles .r4ds-section}
```{r setup,message=FALSE,cache=FALSE}
library("tidyverse")
```
## Exercise 10.1 {.unnumbered .exercise data-number="10.1"}
<div class="question">
How can you tell if an object is a tibble? (Hint: try printing `mtcars`, which is a regular data frame).
</div>
<div class="answer">
When we print `mtcars`, it prints all the columns.
```{r}
mtcars
```
But when we first convert `mtcars` to a tibble using `as_tibble()`, it prints only the first ten observations.
There are also some other differences in formatting of the printed data frame.
It prints the number of rows and columns and the date type of each column.
```{r}
as_tibble(mtcars)
```
You can use the function `is_tibble()` to check whether a data frame is a tibble or not.
The `mtcars` data frame is not a tibble.
```{r}
is_tibble(mtcars)
```
But the `diamonds` and `flights` data are tibbles.
```{r}
is_tibble(ggplot2::diamonds)
is_tibble(nycflights13::flights)
is_tibble(as_tibble(mtcars))
```
More generally, you can use the `class()` function to find out the class of an
object. Tibbles has the classes `c("tbl_df", "tbl", "data.frame")`, while old
data frames will only have the class `"data.frame"`.
```{r}
class(mtcars)
class(ggplot2::diamonds)
class(nycflights13::flights)
```
If you are interested in reading more on R's classes, read the chapters on
object oriented programming in [Advanced R](http://adv-r.had.co.nz/S3.html).
</div>
## Exercise 10.2 {.unnumbered .exercise data-number="10.2"}
<div class="question">
Compare and contrast the following operations on a `data.frame` and equivalent tibble. What is different? Why might the default data frame behaviors cause you frustration?
</div>
<div class="answer">
```{r}
df <- data.frame(abc = 1, xyz = "a")
df$x
df[, "xyz"]
df[, c("abc", "xyz")]
```
```{r}
tbl <- as_tibble(df)
tbl$x
tbl[, "xyz"]
tbl[, c("abc", "xyz")]
```
The `$` operator will match any column name that starts with the name following it.
Since there is a column named `xyz`, the expression `df$x` will be expanded to `df$xyz`.
This behavior of the `$` operator saves a few keystrokes, but it can result in accidentally using a different column than you thought you were using.
With data.frames, with `[` the type of object that is returned differs on the
number of columns. If it is one column, it won't return a data.frame, but
instead will return a vector. With more than one column, then it will return a
data.frame. This is fine if you know what you are passing in, but suppose you
did `df[ , vars]` where `vars` was a variable. Then what that code does
depends on `length(vars)` and you'd have to write code to account for those
situations or risk bugs.
</div>
## Exercise 10.3 {.unnumbered .exercise data-number="10.3"}
<div class="question">
If you have the name of a variable stored in an object, e.g. `var <- "mpg"`, how can you extract the reference variable from a tibble?
</div>
<div class="answer">
You can use the double bracket, like `df[[var]]`. You cannot use the dollar sign, because `df$var` would look for a column named `var`.
</div>
## Exercise 10.4 {.unnumbered .exercise data-number="10.4"}
<div class="question">
Practice referring to non-syntactic names in the following data frame by:
1. Extracting the variable called 1.
1. Plotting a scatterplot of 1 vs 2.
1. Creating a new column called 3 which is 2 divided by 1.
1. Renaming the columns to one, two and three.
</div>
<div class="answer">
For this example, I'll create a dataset called annoying with
columns named `1` and `2`.
```{r}
annoying <- tibble(
`1` = 1:10,
`2` = `1` * 2 + rnorm(length(`1`))
)
```
1. To extract the variable named `1`:
```{r}
annoying[["1"]]
```
or
```{r}
annoying$`1`
```
1. To create a scatter plot of `1` vs. `2`:
```{r}
ggplot(annoying, aes(x = `1`, y = `2`)) +
geom_point()
```
1. To add a new column `3` which is `2` divided by `1`:
```{r}
mutate(annoying, `3` = `2` / `1`)
```
or
```{r}
annoying[["3"]] <- annoying$`2` / annoying$`1`
```
or
```{r}
annoying[["3"]] <- annoying[["2"]] / annoying[["1"]]
```
1. To rename the columns to `one`, `two`, and `three`, run:
```{r}
annoying <- rename(annoying, one = `1`, two = `2`, three = `3`)
glimpse(annoying)
```
</div>
## Exercise 10.5 {.unnumbered .exercise data-number="10.5"}
<div class="question">
What does `tibble::enframe()` do? When might you use it?
</div>
<div class="answer">
The function `tibble::enframe()` converts named vectors to a data frame with names and values
```{r}
enframe(c(a = 1, b = 2, c = 3))
```
</div>
## Exercise 10.6 {.unnumbered .exercise data-number="10.6"}
<div class="question">
What option controls how many additional column names are printed at the footer of a tibble?
</div>
<div class="answer">
The help page for the `print()` method of tibble objects is discussed in `?print.tbl`.
The `n_extra` argument determines the number of extra columns to print information for.
</div>