-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathAS03_know-your-data.Rmd
More file actions
277 lines (202 loc) · 9.95 KB
/
AS03_know-your-data.Rmd
File metadata and controls
277 lines (202 loc) · 9.95 KB
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
---
title: "AS02_Homework_Know-Your-Data"
author: "your name"
editor_options:
markdown:
wrap: 72
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, results = 'hold', comment = '#>', error = TRUE)
```
## 作業目的:
本作業旨在幫助學生熟悉如何讀取不同格式的資料檔案,並使用 **dplyr**
套件進行資料清理、轉換、篩選與基本統計分析。學生將透過兩個實際的資料集:家庭收支調查資料與各鄉鎮市區人口密度資料,來練習資料處理的基本技巧。
## PART I. 家庭收支調查
這個案例將帶你分析家庭消費支出結構(按消費型態分類)。這份資料來自[家庭收支調查](https://data.gov.tw/dataset/6588),記錄了自1976年起的家庭消費支出結構,並根據消費型態進行分類。
- (1)\~(3): 1 point
- (4): 1 point
- (5)\~(6): 1 point
### (1) Read an excel file
1. 首先至政府資料開放平臺上查詢「家庭消費支出結構按消費型態分」,便可找到家庭消費支出資料。點選資料資源下載網址,將檔案存至本機端,直接讀取。
2. 透過政府資料開放平台的 URL或本機端的資料路徑,使用 `read_csv()`
函式將資料讀取至 R 中。
讀取後,列印資料的前3列,並檢查資料結構是否正確。
```{r message=FALSE,warning=FALSE}
### your code
library(tidyverse)
library(readxl)
# Your code here
# head(df_family,n=3)
```
### (2) Rename all columns
1. 照以下對應修改中文變項名稱為英文:
- 年 -\> `year`
- 食品飲料及菸草-百分比 -\> `food_pct`
- 衣著鞋襪類-百分比 -\> `clothes_pct`
- 住宅服務水費瓦斯及其他燃料-百分比 -\> `housing_pct`
- 家具設備及家務服務-百分比 -\> `furniture_pct`
- 醫療保健-百分比 → `health_pct`
- 運輸交通及通訊-百分比 -\> `transport_pct`
- 休閒文化及教育消費-百分比 -\> `leisure_pct`
- 餐廳及旅館-百分比 -\> `restaurants_pct`
- 什項消費-百分比 -\> `other_pct`
2. 刪除「合計」欄位(或選擇除了合計以外的欄位)
3. 將處理過後的結果另存為新的DataFrame: `df_select`
4. 用 `tail()` 印出後五列的結果
5. 提示:可使用dplyr套件中的動詞"select", "rename" 等
```{r message=FALSE,warning=FALSE}
# df_selected<- df_family %>%
# Your code here
# df_selected %>% tail(5)
### result
# # A tibble: 5 × 10
# year food_pct clothes_pct housing_pct furniture_pct health_pct transport_pct leisure_pct restaurants_pct other_pct
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 2019 15.2 2.8 23.6 2.61 16.0 12.2 9.53 12.8 5.22
# 2 2020 15.6 2.75 24.3 2.81 17.0 11.8 7.54 13.1 5.12
# 3 2021 15.8 2.73 24.6 2.94 17.8 12.0 6.34 12.8 5.08
# 4 2022 15.2 2.72 24.6 2.93 17.8 11.6 6.34 13.8 4.96
# 5 2023 15.3 2.65 24.1 2.88 17.7 11.6 6.97 13.9 4.91
```
### (3) Pivot to long table
1. 以 `year`
為主軸,將其他消費百分比的變項,由寬表格(wide-table)轉為長表格(long-table),消費種類的新欄位名稱為
`category` ,消費比例的新欄位名稱為 `percentage` 。
2. 將轉為長表格後的結果另存為新的DataFrame,名為 `toplot`。
3. 用 `head()` 印出前五列
```{r}
# Your code here
### Result should be
# # A tibble: 6 × 3
# year category percentage
# <dbl> <chr> <dbl>
# 1 1976 food_pct 42.3
# 2 1976 clothes_pct 7.2
# 3 1976 housing_pct 22.6
# 4 1976 furniture_pct 3.63
# 5 1976 health_pct 4.85
# 6 1976 transport_pct 5.37
```
::: {#1-5-2}
### (4) Plot
:::
用ggplot繪製出不同家戶消費比例的趨勢變化(你會用哪種圖?全部畫在一張折線圖上?還是你有更好的呈現方法?)
```{r}
# Your code here
```
### (5) Make news report
1. 哪些類別的消費比例是逐年上升?哪些是逐年下降的?你認為為什麼?
::: {#1-5-1}
:::
2. 你認為單獨觀察上升或下降的比例,可能會有什麼樣的問題?請根據這個問題,擬定你應該找什麼專家來採訪?列出專家的名字、職稱、單位和超鏈結。
<div>
</div>
3. 你會怎麼下這個新聞標題?
::: {#1-5-3}
:::
### (6) Add a new column
- 針對「`df_selected`」這個DataFrame,以`health_pct` 為分析欄位,新增
`trend` 欄位。當下一年的「醫療保健-百分比」高於前一年,則在新的
`trend`
欄位中標記為「+」;如果低於或等於前一年,則標記為「-」,最後請列印前5列資料。產生`trend`欄位應該會需要用到`if_else()`函式。
- 用`head()`印出`df_selected`中的`year`、`health_pct`與`trend`欄位的前5列。
- 提示: 可使用dplyr套件中的`lag()`
函式來獲取前一年的「醫療保健-百分比」作為比較的基準。
```{r message=FALSE,warning=FALSE}
# Your code here
### Result should be
# A tibble: 5 × 3
# year health_pct trend
# <dbl> <dbl> <chr>
# 1 1976 4.85 NA
# 2 1977 4.26 -
# 3 1978 4.74 +
# 4 1979 4.62 -
# 5 1980 4.2 -
```
## PART II. 剖析人口資料
請自[政府資料開放平台的各鄉鎮市區人口密度](https://data.gov.tw/dataset/8410),下載最新年度的各鄉鎮市區人口密度統計,其中包含site_id
(區域別)、people_total (年底人口數)、area
(土地面積)和population_density (人口密度)等變項。
- (1)\~(4): 1 point
- (5): 3 points
### (1) Import a csv file
請用 `read_csv()`讀取所下載的 CSV 檔案,取名為 `df_pop`,並用 `head()`
將前 5 列印出。
```{r message=FALSE,warning=FALSE}
### your code
### Result should be
# A tibble: 5 × 5
# statistic_yyy site_id people_total area population_density
# <dbl> <chr> <chr> <dbl> <chr>
# 1 111 新北市板橋區 549572 23.1 23753
# 2 111 新北市三重區 379825 16.3 23278
# 3 111 新北市中和區 403109 20.1 20011
# 4 111 新北市永和區 212170 5.71 37133
# 5 111 新北市新莊區 419966 19.7 21277
```
### (2) Converting strings
查看前5列資料後,你會發現有些欄位的資料型態有點奇怪,本應是數字型態的「人口數」和「人口密度」卻以字串"chr"型態出現,請將這兩欄的資料型態轉換成適合的型態,並再次將
5 列印出
```{r}
### your code
### Result should be
# # A tibble: 5 × 5
# statistic_yyy site_id people_total area population_density
# <dbl> <chr> <int> <dbl> <int>
# 1 111 新北市板橋區 549572 23.1 23753
# 2 111 新北市三重區 379825 16.3 23278
# 3 111 新北市中和區 403109 20.1 20011
# 4 111 新北市永和區 212170 5.71 37133
# 5 111 新北市新莊區 419966 19.7 21277
```
### (3) NA Processing:
1. 執行完前述指令後,你發現console出現警告訊息Warning: NAs introduced
by coercion,(警告: 強制變更過程中產生了
缺失值),請「印出」是那些列的資料出現了`NA`。
2. 請刪除帶有NA值的列後,將資料另存至新的變項`df_pop_cleaned`,並印出修改後的資料有幾列(`df_pop_cleaned`)?
```{r}
### your code
### Result should be
# # A tibble: 2 × 5
# statistic_yyy site_id people_total area population_density
# <dbl> <chr> <int> <dbl> <int>
# 1 111 東沙群島 NA 2.38 NA
# 2 111 南沙群島 NA 0.504 NA
# [1] 368
```
### (4) Sort data
1. 依照人口密度由高至低排序,以`head()`印出六都中人口密度最高的前5個行政區。
```{r}
### Your code here
### Result shoud be
# # A tibble: 5 × 6
# statistic_yyy site_id people_total area population_density county
# <dbl> <chr> <int> <dbl> <int> <chr>
# 1 111 高雄市桃源區 4258 929. 5 高雄
# 2 111 高雄市茂林區 1940 194 10 高雄
# 3 111 臺中市和平區 10919 1038. 11 臺中
# 4 111 高雄市那瑪夏區 3201 253. 13 高雄
# 5 111 新北市烏來區 6359 321. 20 新北
#
# # A tibble: 5 × 6
# statistic_yyy site_id people_total area population_density county
# <dbl> <chr> <int> <dbl> <int> <chr>
# 1 111 新北市永和區 212170 5.71 37133 新北
# 2 111 新北市蘆洲區 199811 7.44 26874 新北
# 3 111 臺北市大安區 284557 11.4 25046 臺北
# 4 111 高雄市新興區 49068 1.98 24827 高雄
# 5 111 新北市板橋區 549572 23.1 23753 新北
```
### (5) Population Increasing (
假設你想知道哪些鄉鎮正在衰落中,也想知道哪些行政區人口正在增加中。
你認為應該以單一鄉鎮來看,還是以縣市為單位來看?如果是以鄉鎮(或縣市)的話,你會如何做計算來呈現這個結果?
::: {#2-5-1}
:::
你的程式碼:
```{r 2-5-2}
# Code should be here
```
說明你發現的結果:
::: {#2-5-3}
:::