# 周次:w12
# 任務:應用(資料框處理與繪圖)
# 姓名:黃子騏
# 日期:2021年4月21日
### 資料框處理
# 請至台灣傳播資料庫下載「2019年調查」的sav檔
# 網址:https://www.crctaiwan.nctu.edu.tw/AnnualSurvey.asp
## 1. 將輸入的sav檔案命名為tcs2019
# install.packages("sjlabelled")
library(sjlabelled)
tcs2019 <- read_spss("tcs2019.sav")

# ## 2. 檢視資料框的各種函數
# # 列數
# #nrow(tcs2019)
# # 檢視資料框內容
# View(tcs2019)
# # 前六行
# head(tcs2019)
# # 後六行
# tail(tcs2019)
# # 欄位名稱或變數名稱
# names(tcs2019)
# # 另一種寫法
# colnames(tcs2019)
# # 得知每個變數的描述性統計量
# summary(tcs2019)
# # 得知資料框複合式的資訊
# # (含資料結構種類、觀察值個數、變數個數、前幾筆觀察值資訊等)
# str(tcs2019)
# ## 欄數
# ncol(tcs2019)
# ## 維度
# dim(tcs2019)
# ## 列的索引值
# row.names(tcs2019)
# # 當資料較大時,建議使用sjPlot套件
# library(sjPlot)
# view_df(tcs2019,
#         file="tcs2019tab.html",  # 結果直接另存新檔
#         show.na = T, # 顯示未重新編碼前的無效值個數
#         show.frq = T, # 顯示次數
#         show.prc = T, # 顯示百分比
#         encoding = "big5"
# )
### 3. 應用實作
# 偵測與處理,讓65+熟齡族告別假新聞危害 
# http://www.crctaiwan.nctu.edu.tw/epaper/%E7%AC%AC202%E6%9C%9F20210409.htm
# RQ1:遇到假新聞的經驗,是否有年齡層的差異存在呢?
# RQ2:對假新聞的感受,是否有年齡層的差異存在呢?
# RQ3:對假新聞的確認與處理方式等,是否有年齡層的差異存在呢?

# (1)確認欲分析的變數
# 年齡 ra2
# 是否有遇到過假新聞? i12.1
# 對假新聞的感受:
# 普遍性 i7a
# 嚴重性 i7b
# 受影響的可能性 i7c
# 確認你接觸到的新聞是不是假新聞? i11.1-i11.8
# 遇到假新聞,你會如何處理? i12.2.1-i12.2.8

# (2)變數整理
# 年齡「變數重新分類」為4類:18-35,36-49,50-64,65UP
# 備註:break的值(x,y,z)是指: group1 >x & <=y; group2 >y & <=z
tcs2019$agegroup <- cut(tcs2019$ra2, breaks = c(17,35,49,64,Inf),
                        labels = c("18至35歲", "36至49歲","50至64歲","65歲以上"))                       
# 檢視各類別有多少人?
table(tcs2019$agegroup)
## 
## 18至35歲 36至49歲 50至64歲 65歲以上 
##      343      534      583      540
# 另一種方法:製作次數分配表
#install.packages("sjmisc")
library(sjmisc)
frq(tcs2019$agegroup, encoding = "big-5", out="v")
x <categorical>
val label frq raw.prc valid.prc cum.prc
18至35歲 343 17.15 17.15 17.15
36至49歲 534 26.70 26.70 43.85
50至64歲 583 29.15 29.15 73.00
65歲以上 540 27.00 27.00 100.00
NA NA 0 0.00 NA NA
total N=2000 · valid N=2000 · x̄=2.66 · σ=1.05
### (3)回答RQ
## RQ1:遇到假新聞的經驗,是否有年齡層的差異存在呢?
## 製表
library(sjPlot)
sjt.xtab(tcs2019$i12.1,tcs2019$agegroup,encoding = "utf-8",show.cell.prc = T,
         show.row.prc = T,
         show.col.prc = T)
I12-1.你過去是否有遇到過假新聞? agegroup Total
18至35歲 36至49歲 50至64歲 65歲以上
有遇到過假新聞 304
20.7 %
88.6 %
15.2 %
426
29 %
79.8 %
21.3 %
429
29.2 %
73.6 %
21.4 %
309
21 %
57.2 %
15.4 %
1468
100 %
73.4 %
73.3 %
從未遇過假新聞 32
7.9 %
9.3 %
1.6 %
87
21.5 %
16.3 %
4.3 %
116
28.6 %
19.9 %
5.8 %
170
42 %
31.5 %
8.5 %
405
100 %
20.2 %
20.2 %
不知道是否遇過假新聞 7
5.5 %
2 %
0.4 %
21
16.5 %
3.9 %
1 %
38
29.9 %
6.5 %
1.9 %
61
48 %
11.3 %
3 %
127
100 %
6.3 %
6.3 %
Total 343
17.1 %
100 %
17.1 %
534
26.7 %
100 %
26.7 %
583
29.1 %
100 %
29.1 %
540
27 %
100 %
27 %
2000
100 %
100 %
100 %
χ2=126.835 · df=6 · Cramer’s V=0.178 · p=0.000
## 製圖
# 1. 變數處理
# (1) 將要繪製的變數變成類別變數或先進行排序
tcs2019$i12.1 <- as.factor(tcs2019$i12.1)
tcs2019$agegroup <- factor(tcs2019$agegroup, ordered = TRUE,
                           levels = c("65歲以上", "50至64歲","36至49歲","18至35歲"))

# 2. 安裝並載入 ggplot2
# 參考 R for Data Science書籍: https://r4ds.had.co.nz/index.html
# 參考ggplot2書籍: https://ggplot2-book.org/index.html
# https://blog.gtwang.org/r/ggplot2-tutorial-layer-by-layer-plotting/3/
# https://rpubs.com/chiahung_tsai/lecture05012018
# https://yijutseng.github.io/DataScienceRBook/vis.html
# https://bookdown.org/jefflinmd38/r4biost/dataviz.html
#install.packages("ggplot2")
# 載入 ggplot2
library(ggplot2)