本文档为学习 R 语言的入门指南,涵盖了安装 R 和 RStudio 的步骤、R 语言的特点、基本语法以及常用的数据科学工具和技术,包括统计建模、数据处理和可视化。文中还通过 A/B 测试举例解释了 R 在数据分析中的应用,同时介绍了 R 的丰富生态系统和众多扩展包。最后,文档强调了 R 作为数据科学完整解决方案的重要性和应用潜力。
安裝 R
▪ 請至http://cran.csie.ntu.edu.tw/ 下載 3.2 版以上的 R
▪ For Windows Users
▪ https://www.youtube.com/watch?v=FsOHPGUIDZU
▪ 注意影片下載的是 3.0.2版,請安裝最新版 (3.2版以上)
▪ For Mac Users
▪ https://www.youtube.com/watch?v=72MYRBNo5Bk
▪ 感謝中華 R 軟體學會的李明昌老師提供影片
▪ For Ubuntu Users
▪ 請參照下列說明
▪ http://cran.csie.ntu.edu.tw/bin/linux/ubuntu/README.html
39
40.
安裝 R Studio
▪R Studio 為 R 的 IDE 環境
▪ 圖形化介面,完整支援 R 的編輯、繪圖以及文件說明
▪ 具備「自動完成」功能
▪ 在 Windows 下支援 UTF-8 的檔案編碼
▪ 請到 R Studio 官方網站
https://www.rstudio.com/products/RStudio/
▪ 下載並安裝 R Studio Desktop Open Source
Edition
40
更多命令
> a <-1
> a
[1] 1
> a + 1
[1] 2
> a = a + 1
[1] 2
> a + 1 -> b
> b
[1] 3
> c(a, b, 1, 11, 21)
[1] 2 3 1 11 21
> 1:5
[1] 1 2 3 4 5
串接數值
賦值 (assignment)
觀察其他資訊區 Environment
分頁下的變化
43
44.
更多命令 (cont.)
> me
Error:object 'me' not found
試試看鍵入 me 之後,不要按 Enter 改按 Tab
試試看鍵入 me 之後,按 Ctrl + C 或 ESC
> "1" + "1"
Error in "1" + "1" : non-numeric argument to
binary operator
試試看鍵入 ?me 之後,按 tab 然後 Enter,看看檔案
系統區出現了什麼
44
45.
使用編輯介面
▪ 左上角選單 File-> New File -> R Script
▪ 在編輯區
▪ 鍵入 1 + 1 按下 Ctrl + Enter,觀察命令列區
▪ 鍵入 1/2 按下 Enter
▪ 鍵入 2*2 按下 Ctrl + Alt + B,觀察命令列區
▪ Tab 鍵在編輯介面下一樣可用
▪ 養成在編輯介面寫程式的習慣
45
list & names
>x <- list(1:5, c("a", "b"))
> x
[[1]]
[1] 1 2 3 4 5
[[2]]
[1] "a" "b"
> attributes(x)
NULL
101
101.
> x <-list(a = 1:5, b = c("a", "b"))
> x
$a
[1] 1 2 3 4 5
$b
[1] "a" "b"
> attributes(x)
$names
[1] "a" "b"
102
list & names (cont.)
102.
$
> x <-list(a = 1:5, b = c("a", "b"))
> x$a
[1] 1 2 3 4 5
> x$b
[1] "a" "b"
103
103.
從 list 到data.frame
▪ list 提供了處理異質資料的工具
▪ R 的物件導向系統就是以 list 為基礎
▪ 將 list 用於結構化資料仍然不甚直覺
▪ 不像矩陣提供許多方便的 API
▪ 所以我們有了 data.frame
▪ 用於結構化資料的 R 物件
▪ 兼具 list 與 matrix 的特性
104
R 的 DataFrame
▪ 結構化資料的典範
The main driver for Distributed DataFrame is to have a
cluster-based, big data representation that’s friendly to the
RDBMSs and data science community. Specifically we
leverage SQL’s table and R’s data.frame concepts, taking
advantage of 30 years of SQL development and R’s
accumulated data science wisdom.
▪ Source: http://ddf.io/design.html
106
R 的繪圖引擎
X11
Unix 作業系統的X-Window GUI
Windows
用於 Windows 作業系統
Quartz
MAC OS X 系統
Postscript
用於印表機或是建立 PS 文件
pdf, png, jpeg
用於建立特定格式的檔案
html 與 javascript
用於建立網頁上的互動式圖表
133
Let’s do it
▪實際利用 R 的基礎繪圖 API 進行單變數與
雙變數的視覺化
▪ 請同學們完成
▪ RVisualization-01-One-Variable-Visualization
▪ RVisualization-02-Two-Variables-Visualization
154
習題題解1 (cont.)
▪ 有事情想不明白,就向資料問答案
>filter(flights, carrier=="AA", !grepl("AA", tailnum)) %>%
"$"("tailnum")
[1] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "“
[23] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "“
[45] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "“
[67] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
> filter(flights, carrier=="AA", !grepl("AA", tailnum)) %>% head()
# A tibble: 6 × 16
year month day dep_time dep_delay arr_time arr_delay carrier tailnum
flight origin dest air_time distance hour minute <int> <int> <int>
<int> <dbl> <int> <dbl> <chr> <chr> <int> <chr> <chr> <dbl> <dbl>
<dbl> <dbl>
1 2013 1 2 NA NA NA NA AA 133 JFK LAX NA 2475 NA NA
2 2013 11 24 NA NA NA NA AA 1185 LGA DFW NA 1389 NA NA
3 2013 11 24 NA NA NA NA AA 1691 EWR DFW NA 1372 NA NA
4 2013 11 25 NA NA NA NA AA 1103 LGA DFW NA 1389 NA NA
5 2013 11 25 NA NA NA NA AA 1107 LGA DFW NA 1389 NA NA
6 2013 11 25 NA NA NA NA AA 1381 EWR DFW NA 1372 NA NA
186