[前言]
事情是這樣的,有一天呢,我需要將我的資料用圖表呈現,我最初一開始,用了 EXCEL 作畫。
之後,老師跟我說:我希望把 X 軸的間距調小,我沒用過 EXCEL,但是我覺得EXCEL應該可以作到。
我都用R。
…好吧,既然老師都這樣說惹,那我只好去用R惹。
[正文開始]
R語言,是一個統計分析的語言,它跟SAS一樣,就是當初設計是用來產生統計圖表與處理資料所使用。
正因為它是開源的專案,因此得到大眾的喜愛與支持。
[開發環境]
比較常用的開發環境:是使用RStudio
[安裝R的方法]
作業系統:Ubuntu 16.04 LTS
1 2 3 4 | sudo apt-get update sudo apt-get install r-base # installing packages sudo apt-get install r-base-dev |
安裝 RStudio
到這個網址,並選擇 Desktop 與 Open Source 的版本即可。
在 Ubuntu 下,下載回來的是一個 deb 檔,雙擊兩下就可以執行安裝了。
Run R script from command line
- use Rscipt
- 在檔案的第一行加入:
1
#!/usr/bin/Rscript
- 範例程式如下:
1 2 | #!/usr/bin/Rscript print( "HelloR" ) |
執行方式:
1 | Rscript test .r |
安裝R的packages
1 | install .packages( "package-name" ) |
在安裝的過程中,有可能需要 mirror (鏡像網站)上的位址。
通常選擇有Taiwan就好(如下示意圖)
載入R的package
1 | library( "package-name" ) |
[基本操作]
寫出第一個小作業:河內塔
基本就是把,一般的C語言,轉換成:R script 的版本。
# Recursive function is about Hanatoai | |
time = 0 | |
recursive.hanatoai <- function(n, charA, charB, charC) { | |
if (n == 1) { | |
theTime = time | |
theTime = theTime + 1 | |
assign("time", theTime, envir = .GlobalEnv) | |
message(time, ": 將第 ", n, " 個圓盤由 ", charA, " 移到 ", charC, "") | |
} | |
else { | |
nextStep = n - 1 | |
recursive.hanatoai(nextStep, charA, charC, charB) | |
theTime = time | |
theTime = theTime + 1 | |
assign("time", theTime, envir = .GlobalEnv) | |
message(time, ": 將第 ", n, " 個圓盤由 ", charA, " 移到 ", charC, "") | |
recursive.hanatoai(nextStep, charB, charA, charC) | |
} | |
} |
這個作業就是來熟悉:基本的遞迴在R的用法(recursive)
[進階應用]
這裡就是之後學習R的各種應用的紀錄。每個都是獨立的文章,在之後會慢慢的更新上去…
讀取某個資料夾檔案,並進行歸類檔案,檔案/資料夾的處理。
R 寫出一個輸入該副檔名就可以以此命名新增資料夾,並將所有有此副檔名的檔案歸類到那個新增資料夾。
第一個是使用DescTools中的StrPos搜尋字串中有沒有我們指定的字串:
library("DescTools") | |
mainDir <- "~" | |
readExtension <- function() | |
{ | |
extFileName <- readline(prompt="Enter an extension file name: ") | |
extFiledir <- file.path(mainDir, extFileName) | |
if(file.exists(extFiledir) == FALSE) { | |
dir.create(extFiledir) | |
} else { | |
unlink(extFiledir, recursive = TRUE) | |
} | |
fileList <- list.files(path = mainDir, pattern = extFileName, recursive = FALSE) | |
index <- 1 | |
while(index<=length(fileList)) { | |
strPosIndex <- StrPos(pattern = extFileName, fileList[index]) | |
if(is.na(strPosIndex) == FALSE) { | |
file.copy(file.path(mainDir, fileList[index]), extFiledir) | |
} | |
index <- index + 1 | |
} | |
} |
第二個是使用stringe中的str_detect從字串中,找到我們要指定的字串:
library("stringr") | |
mainDir <- "~" | |
readExtension <- function() | |
{ | |
extFileName <- readline(prompt="Enter an extension file name: ") | |
extFiledir <- file.path(mainDir, extFileName) | |
if(file.exists(extFiledir) == FALSE) { | |
dir.create(extFiledir) | |
} else { | |
unlink(extFiledir, recursive = TRUE) | |
} | |
fileList <- list.files(path = mainDir, pattern = extFileName, recursive = FALSE) | |
index <- 1 | |
while(index<=length(fileList)) { | |
strPosIndex <- str_detect(fileList[index], pattern = extFileName) | |
if(strPosIndex == TRUE) { | |
file.copy(file.path(mainDir, fileList[index]), extFiledir) | |
} | |
index <- index + 1 | |
} | |
} |
第三個是使用stringe中的str_detect從字串中,找到我們要指定的字串。
跟第二個的差別是,找到檔案名稱有跟附檔名相關的字串,再進一步多一個判斷,判斷是不是附檔名是相等的。(第19行)
因為多這判斷有可能輸入是附檔名是:r,但是判斷檔案名稱可能有*.rb的檔案也算會進去(TRUE)。
所以要再把檔案的附檔名取出來,再去比對輸入的附檔名稱是否一樣。
library("stringr") | |
mainDir <- "~" | |
readExtension <- function() | |
{ | |
extFileName <- readline(prompt="Enter an extension file name: ") | |
extFiledir <- file.path(mainDir, extFileName) | |
if(file.exists(extFiledir) == FALSE) { | |
dir.create(extFiledir) | |
} else { | |
unlink(extFiledir, recursive = TRUE) | |
} | |
fileList <- list.files(path = mainDir, pattern = extFileName, recursive = FALSE) | |
index <- 1 | |
while(index<=length(fileList)) { | |
strPosIndex <- str_detect(fileList[index], pattern = extFileName) | |
if(strPosIndex == TRUE) { | |
extName <- file_ext(fileList[index]) | |
if(extName == extFileName) { | |
file.copy(file.path(mainDir, fileList[index]), extFiledir) | |
} | |
} | |
index <- index + 1 | |
} | |
} |
[其他範例,持續更新中…]
畫圖,讀取CSV檔,畫出曲線圖。(coming soon…..)
畫圖,讀取CSV檔,畫出多條線在同一張圖上。(coming soon…..)
讀取或處理完資料之後,發送 email 做通知。(coming soon…..)
[參考文章]
Run R script from command line on Ubuntu
(下面這個連結,會安裝到新版的R,目前我是用這個方式去安裝)