-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter_15_excercises.Rmd
167 lines (120 loc) · 3.79 KB
/
chapter_15_excercises.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
---
title: "Chapter 15 excercises"
author: "Hugo Åkerstrand"
date: "`r Sys.Date()`"
output: html_document
editor_options:
markdown:
wrap: 72
---
```{r setup, include=TRUE}
library(tidyverse)
library(babynames)
library(ggrepel)
```
### 15.2.4
---
1.
```{r}
exc_1_str <- c(R"(He said "That's amazing!")",
r"(\a\b\c\d)",
r"-(\\\\\\)-"
)
exc_1_str
str_view(exc_1_str)
```
2.
`\u00a0` encodes a "non breaking space", preventing line breaks or that multiple
whitespaces gets collapsed into one.
```{r}
x <- "This\u00a0is\u00a0tricky"
x
str_c(x)
```
### 15.3.4
---
1.
Tidyverse applies rules when it comes to handling NA and recycling, this results
in differences in how the strings are handled:
For the first string, the NA forces the output into NA. For the second, recycling
is only used for single values and therefore throws an error.
```{r, error = TRUE}
str_c("hi ", NA)
str_c(letters[1:2], letters[1:3])
paste0("hi ", NA)
paste0(letters[1:2], letters[1:3])
```
3.
```{r}
food <- "pizza"
price <- 100
age <- 35
country <- "Sweden"
title <- "asskickery"
tribble(
~ input, ~ output,
str_c("The price of ", food, " is ", price), str_glue("The price of {food} is {price}"),
str_glue("I'm {age} years old and live in {country}"), str_c("I'm", age, "years old and live in", country, sep = " "),
str_c("\\section{", title, "}"), str_glue("\\section{{{title}}}")
)
```
### 15.5.3
---
1.
The `babynames` data set reports the observed name for a given year, gender,
and number of individuals with this specific name. As such, it is necessary
to specify `wt = n` to get the sum of names with a specific length.
2.
```{r}
babynames |>
mutate(
name_length = str_length(name),
name_mid_position = if_else(
name_length %% 2 == 0,
NA,
ceiling(name_length / 2)
),
name_mid_letter = str_sub(
name,
start = name_mid_position,
end = name_mid_position
)
)
```
3.
```{r}
#Add some summaries that will be interesting to analyze
babynames_analysis <- babynames |>
mutate(
name_length = str_length(name),
name_first_letter = str_sub(name, 1, 1),
name_last_letter = str_sub(name, -1, -1),
name_rank = dense_rank(desc(prop)),
.by = c(year, sex)
) |>
arrange(year, name_rank)
#Box plot to identify top 3 popular names from oldest (1880) and newest (2017) year
babynames_analysis |>
filter(year %in% c(1880, max(year))) |>
ggplot(aes(y = prop, x = sex)) +
geom_boxplot() +
geom_text_repel(data = babynames_analysis |> filter(year %in% c(1880, max(year))) |> filter(name_rank <= 3), aes(label = name)) +
facet_wrap( ~ year)
#Save the favorite names to a new object to call it for subsetting the line graph
favorite_names_old_modern <- babynames_analysis |>
filter(year %in% 1880 | year %in% 2017) |>
filter(name_rank <= 3) |>
select(year, name, sex)
#Draw the line graph to track popularity over years
babynames_analysis |>
mutate(popularity = case_when( #Add new variable for facetting
name %in% pull(filter(favorite_names_old_modern, year == 2017), name) ~ "Modern favorite",
name %in% pull(filter(favorite_names_old_modern, year == 1880), name) ~ "Old school favorite"
)) |>
filter(name %in% pull(filter(favorite_names_old_modern, sex == "F"), name) & sex == "F" |
name %in% pull(filter(favorite_names_old_modern, sex == "M"), name) & sex == "M"
) |> #Filter out names given female or male sex
ggplot(aes(x = year, y = prop, color = name)) +
geom_line(aes(color = name)) +
facet_grid(popularity ~ sex)
```