聯合國投票資料集

案例研究:R 中的探索性資料分析

Dave Robinson

Chief Data Scientist, DataCamp

UN 投票資料集

rcid session vote ccode
46 2 1 2
46 2 1 20
46 2 9 31
46 2 1 40
46 2 1 41
46 2 1 42
46 2 1 51
46 2 9 52
46 2 9 53
1 Erik Voeten,「Data and Analyses of Voting in the UN General Assembly」
案例研究:R 中的探索性資料分析

UN 投票資料集

rcid session vote ccode
46 2 1 2 每列是一個國家-投票配對
46 2 1 20
46 2 9 31
46 2 1 40
46 2 1 41
46 2 1 42
46 2 1 51
46 2 9 52
46 2 9 53
1 Erik Voeten,「Data and Analyses of Voting in the UN General Assembly」
案例研究:R 中的探索性資料分析

UN 投票資料集

rcid session vote ccode
46 2 1 2 每列是一個國家-投票配對
46 2 1 20
46 2 9 31 rcid = 「點名投票 ID」
46 2 1 40
46 2 1 41
46 2 1 42
46 2 1 51
46 2 9 52
46 2 9 53
1 Erik Voeten,「Data and Analyses of Voting in the UN General Assembly」
案例研究:R 中的探索性資料分析

UN 投票資料集

rcid session vote ccode
46 2 1 2 每列是一個國家-投票配對
46 2 1 20
46 2 9 31 rcid = 點名投票 ID
46 2 1 40
46 2 1 41 session = 會期年份
46 2 1 42
46 2 1 51
46 2 9 52
46 2 9 53
1 Erik Voeten,「Data and Analyses of Voting in the UN General Assembly」
案例研究:R 中的探索性資料分析

UN 投票資料集

rcid session vote ccode
46 2 1 2 每列是一個國家-投票配對
46 2 1 20
46 2 9 31 rcid = 點名投票 ID
46 2 1 40
46 2 1 41 session = 會期年份
46 2 1 42
46 2 1 51 vote = 投票代碼
46 2 9 52
46 2 9 53
1 Erik Voeten,「Data and Analyses of Voting in the UN General Assembly」
案例研究:R 中的探索性資料分析

UN 投票資料集

rcid session vote ccode
46 2 1 2 每列是一個國家-投票配對
46 2 1 20
46 2 9 31 rcid = 點名投票 ID
46 2 1 40
46 2 1 41 session = 會期年份
46 2 1 42
46 2 1 51 vote = 投票代碼
46 2 9 52
46 2 9 53 ccode = 國家代碼
1 Erik Voeten,「Data and Analyses of Voting in the UN General Assembly」
案例研究:R 中的探索性資料分析

在 dplyr 中檢視投票資料

# Load dplyr package
library(dplyr)
votes
# A tibble: 508,929 × 4
    rcid session  vote ccode
   <dbl>   <dbl> <dbl> <int>
1     46       2     1     2
2     46       2     1    20
3     46       2     9    31
4     46       2     1    40
5     46       2     1    41
6     46       2     1    42
7     46       2     9    51
8     46       2     9    52
9     46       2     9    53
10    46       2     9    54
# ... with 508,919 more rows

變數名稱

案例研究:R 中的探索性資料分析

管線運算子(pipe)

管線運算子 1-1.014.png

案例研究:R 中的探索性資料分析

管線運算子(pipe)

管線運算子 1-1.015.png

案例研究:R 中的探索性資料分析

dplyr 動詞(verbs)

dplyr 動詞 1-1.018.png

案例研究:R 中的探索性資料分析

dplyr 動詞(verbs)

dplyr 動詞 1-1.020.png

案例研究:R 中的探索性資料分析

原始資料

votes
# A tibble: 508,929 × 4
    rcid session  vote ccode
   <dbl>   <dbl> <dbl> <int>
1     46       2     1     2
2     46       2     1    20
3     46       2     9    31
4     46       2     1    40
5     46       2     1    41
6     46       2     1    42
7     46       2     9    51
8     46       2     9    52
9     46       2     9    53
10    46       2     9    54
# ... with 508,919 more rows
1 = 贊成
2 = 棄權
3 = 反對
8 = 未出席
9 = 非會員
案例研究:R 中的探索性資料分析

dplyr 動詞:filter

filter 依條件保留觀測值

votes %>%
  filter(vote <= 3)
# A tibble: 353,547 × 4
    rcid session  vote ccode
   <dbl>   <dbl> <dbl> <int>
1     46       2     1     2
2     46       2     1    20
3     46       2     1    40
4     46       2     1    41
5     46       2     1    42
6     46       2     1    70
7     46       2     1    90
8     46       2     1    91
9     46       2     1    92
10    46       2     1    93
# ... with 508,919 more rows
案例研究:R 中的探索性資料分析

dplyr 動詞:mutate

mutate 新增一個變數

votes %>%
  mutate(year = session + 1945)
# A tibble: 508,929 × 5
    rcid session  vote ccode  year
   <dbl>   <dbl> <dbl> <int> <dbl>
1     46       2     1     2  1947
2     46       2     1    20  1947
3     46       2     9    31  1947
4     46       2     1    40  1947
5     46       2     1    41  1947
6     46       2     1    42  1947
7     46       2     9    51  1947
8     46       2     9    52  1947
9     46       2     9    53  1947
10    46       2     9    54  1947
# ... with 508,919 more rows
案例研究:R 中的探索性資料分析

資料清理中的串接操作

資料清理中的串接操作 1-1.033.png

案例研究:R 中的探索性資料分析

一起來練習吧!

案例研究:R 中的探索性資料分析

Preparing Video For Download...