-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtidyverse_tidyr2.Rmd
286 lines (199 loc) · 4.47 KB
/
tidyverse_tidyr2.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# 数据规整2 {#tidyverse-tidyr2}
接着上一章,罗列一些`tidyr`的函数
```{r, message = FALSE, warning = FALSE}
library(tidyverse)
```
## `fill()` 缺失值填充
利用**所在列**的上下值进行缺失值填充
```{r}
sales <- tibble::tribble(
~quarter, ~year, ~sales,
"Q1", 2000, 66013,
"Q2", NA, 69182,
"Q3", NA, 53175,
"Q4", NA, 21001,
"Q1", 2001, 46036,
"Q2", NA, 58842,
"Q3", NA, 44568,
"Q4", NA, 50197,
"Q1", 2002, 39113,
"Q2", NA, 41668,
"Q3", NA, 30144,
"Q4", NA, 52897
)
sales
```
```{r}
sales %>% fill(year)
```
也可以控制填充的方向
```{r}
sales %>% fill(year, .direction = "up")
```
## `expand()` 与 `complete()`
指定数据框的若干列,根据其向量元素值,产生所有可能的交叉组合
```{r}
df <- tibble::tribble(
~x, ~y, ~z,
1L, 1L, 4L,
1L, 2L, 5L,
2L, 1L, NA,
3L, 2L, 6L
)
df %>% expand(x, y)
```
`nesting()`用于限定只产生数据框已出现的组合。
```{r}
df %>% expand(nesting(x, y))
```
```{r}
df %>% expand(nesting(x, y), z)
```
`complete()` 补全,可以看做是 `expand(nesting()) + fill()`
```{r}
df %>% complete(x, y)
```
```{r}
df %>% complete(x, y, fill = list(z = 0))
```
数据在complete补全的时候,会面临有两种缺失值:
1. 补位的时候造成的空缺
2. 数据原先就存在缺失值
```{r}
df %>% complete(x, y)
```
- 补位的时候造成的空缺,可通过`fill = list(x = 0)` 控制填充
```{r}
df %>% complete(x, y, fill = list(z = 0))
```
- 数据原先就存在缺失值,最好通过 `explicit = FALSE`显式地控制是否填充
```{r}
df %>% complete(x, y, fill = list(z = 0), explicit = FALSE)
```
## `expand_grid()` 与 `crossing()`
产生一个新的数据框,每行对应着向量元素的所有交叉组合
```{r}
expand_grid(x = 1:3, y = 1:2)
```
```{r}
crossing(x = 1:3, y = 1:2)
```
向量换成数据框也可以,其结果就是数据框行与元素的交叉组合
```{r}
expand_grid(df = data.frame(x = 1:2, y = c(2, 1)), z = 1:3)
```
```{r}
crossing(df = data.frame(x = 1:2, y = c(2, 1)), z = 1:3)
```
`crossing()`可以看作是`expand_grid() + distinct()`, 即`crossing()`在完成交叉组合之后会自动去重,比如
```{r}
expand_grid(x = c(1, 1), y = c(1:2)) # 不考虑去重
```
```{r}
crossing(x = c(1, 1), y = c(1:2)) # 考虑去重
```
## `separate()` 与 `unite()`
```{r}
tb <- tibble::tribble(
~day, ~price,
1, "30-45",
2, "40-95",
3, "89-65",
4, "45-63",
5, "52-42"
)
```
```{r}
tb1 <- tb %>%
separate(price, into = c("low", "high"), sep = "-")
tb1
```
```{r}
tb1 %>%
unite(col = "price", c(low, high), sep = ":", remove = FALSE)
```
有时候分隔符搞不定的,可以用正则表达式,将捕获的每组弄成一列
```{r}
dfc <- tibble(x = c("1-12week", "1-10wk", "5-12w", "01-05weeks"))
dfc
```
```{r}
dfc %>% tidyr::extract(
x,
c("start", "end", "letter"), "(\\d+)-(\\d+)([a-z]+)",
remove = FALSE
)
```
## 删除缺失值所在行drop_na()与replace_na()
```{r}
df <- tibble::tribble(
~name, ~type, ~score, ~extra,
"Alice", "english", 80, 10,
"Alice", "math", NA, 5,
"Bob", "english", NA, 9,
"Bob", "math", 69, NA,
"Carol", "english", 80, 10,
"Carol", "math", 90, 5
)
df
```
如果score列中有缺失值`NA`,就删除所在的row
```{r}
df %>%
filter(!is.na(score))
```
或者用`across()`
```{r}
df %>%
filter(
across(score, ~ !is.na(.x))
)
```
所有列,如果有缺失值`NA`,就删除所在的row
```{r}
df %>%
filter(
across(everything(), ~ !is.na(.x))
)
```
现在有更简便的方法
```{r}
df %>%
drop_na()
```
也可指定某一列
```{r}
df %>%
drop_na(score)
```
没来参加考试,视为0分,可以用`replace_na()`
```{r}
df %>% mutate(score = replace_na(score, 0))
```
或者使用`coalesce()`
```{r}
df %>% mutate(score = coalesce(score, 0))
```
```{r}
df %>%
mutate(
across(c(score, extra), ~ coalesce(.x, 0))
)
```
没来参加考试,用平均分代替
```{r}
df %>%
mutate(
score = replace_na(score, mean(score, na.rm = TRUE))
)
```
当然也可以用`if_else()`来做
```{r}
df %>%
mutate(
score = if_else(is.na(score), mean(score, na.rm = TRUE), score)
)
```
```{r, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```