-
Notifications
You must be signed in to change notification settings - Fork 10
/
ch14.Rmd
178 lines (130 loc) · 3.78 KB
/
ch14.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
---
title: "使用 dplyr 套件整理資料"
author: "郭耀仁"
date: "`r Sys.Date()`"
output: slidy_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, results = 'hide', message = FALSE)
```
## 安裝與載入
```
install.packages("dplyr")
```
```{r}
library(dplyr)
```
## 不同的函數
|函數|用途|
|:----|:----|
|`filter()`|觀測值(列)選擇|
|`select()`|變數(欄)選擇|
|`mutate()`|新增變數(欄)|
|`arrange()`|排序|
|`summarise()`|聚合|
|`group_by()`|分組|
## `filter()`
- 選出女性船員
```{r}
load(url("https://storage.googleapis.com/r_rookies/straw_hat_df.RData"))
filter(straw_hat_df, gender == "Female")
```
## `filter()`(2)
- 選出女性船員並且年紀大於等於 30 歲
```{r}
load(url("https://storage.googleapis.com/r_rookies/straw_hat_df.RData"))
filter(straw_hat_df, gender == "Female" & age >= 30)
```
## `select()`
- 選擇資料框中的變數
```{r}
load(url("https://storage.googleapis.com/r_rookies/straw_hat_df.RData"))
select(straw_hat_df, name, gender)
```
## 使用 `%>%` 運算子
- 可以操作成像是 SQL 的查詢指令
```{r}
# 選出女性船員,但只回傳 name 變數
load(url("https://storage.googleapis.com/r_rookies/straw_hat_df.RData"))
straw_hat_df %>%
filter(gender == "Female") %>%
select(name)
```
## `mutate()`
- 新增出生年(`birth_year`)變數
- 利用系統日期減去年紀
```{r}
load(url("https://storage.googleapis.com/r_rookies/straw_hat_df.RData"))
straw_hat_df %>%
mutate(birth_year = as.integer(format(Sys.Date(), "%Y")) - age)
```
## `arrange()`
- 新增出生年(`birth_year`)變數後以出生年排序
```{r}
load(url("https://storage.googleapis.com/r_rookies/straw_hat_df.RData"))
straw_hat_df %>%
mutate(birth_year = as.integer(format(Sys.Date(), "%Y")) - age) %>%
arrange(birth_year)
# arrange(desc(birth_year))
```
## `summarise()`
- 計算聚合變數平均年齡 `mean_age`
```{r}
load(url("https://storage.googleapis.com/r_rookies/straw_hat_df.RData"))
summarise(straw_hat_df, mean_age = mean(age))
```
## `group_by()`
- 依照性別計算平均年齡 `mean_age`
```{r}
load(url("https://storage.googleapis.com/r_rookies/straw_hat_df.RData"))
straw_hat_df %>%
group_by(gender) %>%
summarise(mean_age = mean(age))
```
## `join` 系列函數
|種類|函數|
|----|----|
|內部連結|`inner_join()`|
|左外部連結|`left_join()`|
|右外部連結|`right_join()`|
|全外部連結|`full_join()`|
## `inner_join()`
```{r}
library(dplyr)
# 左表格 straw_hat_df
load(url("https://storage.googleapis.com/r_rookies/straw_hat_df.RData"))
# 右表格 straw_hat_devil_fruit
load(url("https://storage.googleapis.com/r_rookies/straw_hat_devil_fruit.RData"))
# 內部連結
joined_df <- inner_join(straw_hat_df, straw_hat_devil_fruit)
```
## `left_join()`
```{r}
library(dplyr)
# 左表格 straw_hat_df
load(url("https://storage.googleapis.com/r_rookies/straw_hat_df.RData"))
# 右表格 straw_hat_devil_fruit
load(url("https://storage.googleapis.com/r_rookies/straw_hat_devil_fruit.RData"))
# 左外部連結
joined_df <- left_join(straw_hat_df, straw_hat_devil_fruit)
```
## `right_join()`
```{r}
library(dplyr)
# 左表格 straw_hat_df
load(url("https://storage.googleapis.com/r_rookies/straw_hat_df.RData"))
# 右表格 straw_hat_devil_fruit
load(url("https://storage.googleapis.com/r_rookies/straw_hat_devil_fruit.RData"))
# 右外部連結
joined_df <- right_join(straw_hat_df, straw_hat_devil_fruit)
```
## `full_join()`
```{r}
library(dplyr)
# 左表格 straw_hat_df
load(url("https://storage.googleapis.com/r_rookies/straw_hat_df.RData"))
# 右表格 straw_hat_devil_fruit
load(url("https://storage.googleapis.com/r_rookies/straw_hat_devil_fruit.RData"))
# 全外部連結
joined_df <- full_join(straw_hat_df, straw_hat_devil_fruit)
```