# 周次:w
# 任務:應用(資料框處理與繪圖)
# 姓名:陳靜君
# 日期:xxxx年xx月xx日
### 資料框處理
# 請至台灣傳播資料庫下載「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)
## Learn more about sjPlot with 'browseVignettes("sjPlot")'.
sjt.xtab(tcs2019$agegroup,tcs2019$i12.1,encoding="utf-8")
|
agegroup
|
I12-1.你過去是否有遇到過假新聞?
|
Total
|
|
有遇到過假新聞
|
從未遇過假新聞
|
不知道是否遇過假新聞
|
|
18至35歲
|
304
|
32
|
7
|
343
|
|
36至49歲
|
426
|
87
|
21
|
534
|
|
50至64歲
|
429
|
116
|
38
|
583
|
|
65歲以上
|
309
|
170
|
61
|
540
|
|
Total
|
1468
|
405
|
127
|
2000
|
χ2=126.835 · df=6 · Cramer’s V=0.178 · p=0.000
|
# sjt.xtab(tcs2019$i12.1,tcs2019$agegroup,encoding="utf-8")=先烈在藍
# # sjt.xtab(tcs2019$i12.1,tcs2019$agegroup,encoding = "utf-8",show.cell.prc = T,
# show.row.prc = T,
# show.col.prc = T)
## 製圖
# 1. 變數處理
# (1) 將要繪製的變數變成類別變數或先進行排序
tcs2019$i12.1 <- as.factor(tcs2019$i12.1)
class(tcs2019$i12.1)
## [1] "factor"
# 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)