- 安裝R 與Rstudio
- Rstudio 功能簡介
- Expression、變數與字串
- 函數的使用
- 套件的使用
- R 語言的基礎物件與型態
- R 語言的錯誤處理與除錯
- 安裝R 語言翻轉教室
- R Basic 系列習題解說
2019/5/8
expression
(敘述)
expression
>
代表「R 正準備接受下一個expression」Enter
後,R會檢查expression有沒有結束
+
提示使用者expression尚未結束expression
expression
expression
+
代表「R 認為expression沒有結束」ESC
可以中斷expressionexpression
#
之後的程式碼不會被處理,稱為「註解」1 + 1
## [1] 2
#1 + 1
expression
expression
中,可能包含多個子expression
expression
的運算結果與子expression
本身做替換((( 1 + 1 ) + 1) + 1) ((2 + 1) + 1) (3 + 1) 4
expression
會用怎樣的順序做運算?:
、*
、^
與%%
都是運算子??operator
3 ^ 2 * 2 + 10 %% 3
=
或<-
來建立變數變數的名稱 = expression
y <- x + 1
x
代入 1 則 y
為 2x
代入 2 則 y
為 3x <- 1 ### y <- x + 1 y <- 1 + 1 y <- 2
x <- 10 (1 + 2 * x) (1 + 2 * 10)
A syntactic name is a string the parser interprets as this type of expression. It consists of letters, numbers, and the dot and (for versions of R at least 1.9.0) underscore characters, and starts with either a letter or a dot not followed by a number. Reserved words are not syntactic names.
x <- 1 .x <- 1 _x <- 1 x_1 <- 1 . <- 1 .1 <- 1 中文 <- 1
"
或'
所夾住的文字,在程式碼中會視為字串(character)1 + 1
## [1] 2
"1 + 1"
## [1] "1 + 1"
"1" + '1' # 文字不可相加
## Error in "1" + "1": 二元運算子中有非數值引數
a <- 1 plot.new() title(main = "a")
plot.new() title(main = a) # 變數會有「代換」的動作
plot.new() title(main = "a")
plot.new
、title
等是R的函數plot.new
代表建立一個空白的圖、title
代表建立標題變數的名字
函數的名字(...)
install.packages("caTools") # install external package library(caTools) # external package providing write.gif function jet.colors <- colorRampPalette(c("red", "blue", "#007FFF", "cyan", "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000")) dx <- 1500 # define width dy <- 1400 # define height C <- complex(real = rep(seq(-2.2, 1.0, length.out = dx), each = dy), imag = rep(seq(-1.2, 1.2, length.out = dy), dx)) C <- matrix(C, dy, dx) # reshape as square matrix of complex numbers Z <- 0 # initialize Z to zero X <- array(0, c(dy, dx, 20)) # initialize output 3D array for (k in 1:20) { # loop with 20 iterations Z <- Z^2 + C # the central difference equation X[, , k] <- exp(-abs(Z)) # capture results } write.gif(X, "Mandelbrot.gif", col = jet.colors, delay = 100)
install.packages("caTools") # install external package
install.packages
是函數的名字,告訴R要做什麼動作"caTools"
是install.packages
的參數,告訴R 動作的細節expression
expression
C <- complex(real = rep(seq(-2.2, 1.0, length.out = dx), each = dy), imag = rep(seq(-1.2, 1.2, length.out = dy), dx))
complex
的函數時,指定了名稱為real
與imag
的兩個參數
real
的參數的值是rep(seq(-2.2, 1.0, length.out = dx), each = dy)
這個expression
rep
與seq
也都是函數expression
中有三層的函數呼叫(巢狀函數)real
的值中的seq
函數的前兩個參數-2.2
與1.0
沒有名字,第三個參數的名字是length.out
?seq
Usage
章節說明函數的定義## Default S3 method: seq(from = 1, to = 1, by = ((to - from)/(length.out - 1)), length.out = NULL, along.with = NULL, ...)
參數的名字
或 參數的名字 = 預設值
...
是不定參數,表示函數接受額外的參數(註:除了列出的名字以外的參數)plot.new()
plot.new(main = "a") # Error
## Error in plot.new(main = "a"): unused argument (main = "a")
seq()
## [1] 1
seq(lalalalove = 1) # No error
## Warning: In seq.default(lalalalove = 1) : ## extra argument 'lalalalove' will be disregarded
## [1] 1
seq(-2.2, 1.0, length.out = dx)
dx
是length.out
參數的值-2.2
是from
的值1.0
是to
的值mean()
## Error in mean.default(): 缺少引數 "x",也沒有預設值
debug(seq) seq(-2.2, 1.0, length.out = dx)
dx <- 1500 # define width dy <- 1400 # define height C <- complex(real = rep(seq(-2.2, 1.0, length.out = dx), each = dy), imag = rep(seq(-1.2, 1.2, length.out = dy), dx))
C
之前,R 會先計算seq
函數的結果,然後計算rep
函數的結果,最後才計算complex
函數的結果expression
進行檢查XML
、xml2
、jsonlite
httr
supc
dplyr
ggplot2
Lahman
install.packages
函數?install.packages
pkgs
,代表要安裝的套件的名稱
pkgs
的參數要不要加上字串library
函數write.gif
是caTools
的函數caTools::write.gif
來使用(套件名稱::函數名稱
)library(caTools)
library
的參數不需要加上引號
探索套件 –> 探索函數
重現範例 –> 修改範例
DESCRIPTION
vignettes
install.packages("caTools") # install external package library(caTools) # external package providing write.gif function jet.colors <- colorRampPalette(c("red", "blue", "#007FFF", "cyan", "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000")) dx <- 1500 # define width dy <- 1400 # define height C <- complex(real = rep(seq(-2.2, 1.0, length.out = dx), each = dy), imag = rep(seq(-1.2, 1.2, length.out = dy), dx)) C <- matrix(C, dy, dx) # reshape as square matrix of complex numbers Z <- 0 # initialize Z to zero X <- array(0, c(dy, dx, 20)) # initialize output 3D array for (k in 1:20) { # loop with 20 iterations Z <- Z^2 + C # the central difference equation X[, , k] <- exp(-abs(Z)) # capture results } write.gif(X, "Mandelbrot.gif", col = jet.colors, delay = 100)
x <- 1:3 # x 是一個R物件,是一個向量(不只一個值) x[1] # 第一個值
## [1] 1
x[2] # 第二個值
## [1] 2
length(x) # 長度
## [1] 3
logical
裝布林(TRUE
/FALSE
)的向量integer
裝整數(4 bytes)的向量numeric
裝浮點數(8 bytes)的向量character
裝字串(C-style)的向量list
裝R物件的向量class
可以查詢物件的型態、str
可以查詢物件的結構g <- lm(dist ~ speed, cars) str(g)
## List of 12 ## $ coefficients : Named num [1:2] -17.58 3.93 ## ..- attr(*, "names")= chr [1:2] "(Intercept)" "speed" ## $ residuals : Named num [1:50] 3.85 11.85 -5.95 12.05 2.12 ... ## ..- attr(*, "names")= chr [1:50] "1" "2" "3" "4" ... ## $ effects : Named num [1:50] -303.914 145.552 -8.115 9.885 0.194 ... ## ..- attr(*, "names")= chr [1:50] "(Intercept)" "speed" "" "" ... ## $ rank : int 2 ## $ fitted.values: Named num [1:50] -1.85 -1.85 9.95 9.95 13.88 ... ## ..- attr(*, "names")= chr [1:50] "1" "2" "3" "4" ... ## $ assign : int [1:2] 0 1 ## $ qr :List of 5 ## ..$ qr : num [1:50, 1:2] -7.071 0.141 0.141 0.141 0.141 ... ## .. ..- attr(*, "dimnames")=List of 2 ## .. .. ..$ : chr [1:50] "1" "2" "3" "4" ... ## .. .. ..$ : chr [1:2] "(Intercept)" "speed" ## .. ..- attr(*, "assign")= int [1:2] 0 1 ## ..$ qraux: num [1:2] 1.14 1.27 ## ..$ pivot: int [1:2] 1 2 ## ..$ tol : num 1e-07 ## ..$ rank : int 2 ## ..- attr(*, "class")= chr "qr" ## $ df.residual : int 48 ## $ xlevels : Named list() ## $ call : language lm(formula = dist ~ speed, data = cars) ## $ terms :Classes 'terms', 'formula' language dist ~ speed ## .. ..- attr(*, "variables")= language list(dist, speed) ## .. ..- attr(*, "factors")= int [1:2, 1] 0 1 ## .. .. ..- attr(*, "dimnames")=List of 2 ## .. .. .. ..$ : chr [1:2] "dist" "speed" ## .. .. .. ..$ : chr "speed" ## .. ..- attr(*, "term.labels")= chr "speed" ## .. ..- attr(*, "order")= int 1 ## .. ..- attr(*, "intercept")= int 1 ## .. ..- attr(*, "response")= int 1 ## .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> ## .. ..- attr(*, "predvars")= language list(dist, speed) ## .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric" ## .. .. ..- attr(*, "names")= chr [1:2] "dist" "speed" ## $ model :'data.frame': 50 obs. of 2 variables: ## ..$ dist : num [1:50] 2 10 4 22 16 10 18 26 34 17 ... ## ..$ speed: num [1:50] 4 4 7 7 8 9 10 10 10 11 ... ## ..- attr(*, "terms")=Classes 'terms', 'formula' language dist ~ speed ## .. .. ..- attr(*, "variables")= language list(dist, speed) ## .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1 ## .. .. .. ..- attr(*, "dimnames")=List of 2 ## .. .. .. .. ..$ : chr [1:2] "dist" "speed" ## .. .. .. .. ..$ : chr "speed" ## .. .. ..- attr(*, "term.labels")= chr "speed" ## .. .. ..- attr(*, "order")= int 1 ## .. .. ..- attr(*, "intercept")= int 1 ## .. .. ..- attr(*, "response")= int 1 ## .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> ## .. .. ..- attr(*, "predvars")= language list(dist, speed) ## .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric" ## .. .. .. ..- attr(*, "names")= chr [1:2] "dist" "speed" ## - attr(*, "class")= chr "lm"
x <- 1:3 # `:` 可以快速產生序列,`1:3` 代表從1至3間隔為1的數列 y <- 2:4 x + y
## [1] 3 5 7
p
是不是質數p <- 257 . <- 2:(p-1) # 2 和 p-1之間的所有整數 # 檢查他們是不是p的因數(餘數為0) . <- p %% . # 計算餘數 . <- . == 0 # 拿餘數與0比較 any(.) # 有沒有任何的因數,有因數的話p就不是值數
## [1] FALSE
# which(.) # 看看那一個是因數
x <- 1:3 y <- 1:6 x + y # R會重複比較短的向量,補到一樣長之後再做運算
## [1] 2 4 6 5 7 9
[
的操作[
可以從向量中讀出特定的資料[
是一種特別的函數
[
的左邊[
與]
之間[
可以與 <-
或=
搭配,修改向量的值[
+ 數值向量x[1]
會從x
中取出第一個位置的元素x <- 1:3 x[1]
## [1] 1
x[1:2]
## [1] 1 2
x[-2]
## [1] 1 3
[
+ 布林向量x[c(T, F, T)]
會取出對應位置為T
的位置的元素set.seed(1) x <- rnorm(10) # 10000個標準常態分佈的隨機亂數 x[x > 1] # 超過1的元素
## [1] 1.595281
x[x > -1 & x < 1] # 1 與 -1 之間的元素
## [1] -0.6264538 0.1836433 -0.8356286 0.3295078 -0.8204684 0.4874291 ## [7] 0.7383247 0.5757814 -0.3053884
y <- rnorm(10) x[y > 1] # 那些y超過1的對應位置的x
## [1] -0.6264538 0.3295078
[
+ 字串向量x <- 1:26 names(x) <- letters x["n"]
## n ## 14
x[c("n", "w", "w", "a")]
## n w w a ## 14 23 23 1
[
x[...]
一定與x
會是相同的型態[
的效果與x
的型態無關,與[]
之間的物件的型態有關source("http://homepage.ntu.edu.tw/~wush978/R/init-swirl.R")
list
[[
或$
才能接觸到裝載的向量g <- lm(dist ~ speed, cars) g[[1]]
## (Intercept) speed ## -17.579095 3.932409
g$coefficients
## (Intercept) speed ## -17.579095 3.932409
list
g[1]
## $coefficients ## (Intercept) speed ## -17.579095 3.932409
list(g[[1]])
## [[1]] ## (Intercept) speed ## -17.579095 3.932409
g[[1]]
## (Intercept) speed ## -17.579095 3.932409
list
」. <- g[[1]] attributes(.)
## $names ## [1] "(Intercept)" "speed"
str(.)
## Named num [1:2] -17.58 3.93 ## - attr(*, "names")= chr [1:2] "(Intercept)" "speed"
str
了解R 物件的結構str(g[1])
## List of 1 ## $ coefficients: Named num [1:2] -17.58 3.93 ## ..- attr(*, "names")= chr [1:2] "(Intercept)" "speed"
list
與樹狀結構parent ---> children
list
與樹狀結構list
與樹狀結構list
與樹狀結構list
與樹狀結構list
可以視為建立一個R物件之間的樹狀結構list(2, 3, 4, 5)
## list
與樹狀結構
x <- list("2" = list(5, 6, 7), "3" = list(8, 9, 10), "4" = list(11, 12, 13))
{ "name" : "Peter", "age" : 35 "children" : [{"name" : "George", "age" : 5}, {"name" : "Mary", "age" : 3}] }
[
與 [[
[
: 停留在parent,但是挑選children
[[
: 往children
走x[1]
## $`2` ## $`2`[[1]] ## [1] 5 ## ## $`2`[[2]] ## [1] 6 ## ## $`2`[[3]] ## [1] 7
[
與 [[
[
: 停留在parent,但是挑選children
[[
: 往children
走x[[1]] # x$`2`
## [[1]] ## [1] 5 ## ## [[2]] ## [1] 6 ## ## [[3]] ## [1] 7
character
型態data.frame
時(R 常用的,處理結構化資料的物件)會自動轉換字串為factor
型態x <- iris$Species # 內建的結構化資料集 str(x)
## Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
attributes(x)
## $levels ## [1] "setosa" "versicolor" "virginica" ## ## $class ## [1] "factor"
attributes(x) <- NULL str(x)
## int [1:150] 1 1 1 1 1 1 1 1 1 1 ...
f <- function() { print(1) # 正常執行 stop("test error") # `stop` 拋出 condition 物件,中斷`f`的運作 print(2) # `f`被中斷了,不會被執行 } f()
## [1] 1
## Error in f(): test error
try
g <- function() { f() print(3) # 不會被執行 } g()
## [1] 1
## Error in f(): test error
g <- function() { try(f(), silent = TRUE) # 處理錯誤後,會繼續執行`g` print(3) # } g()
## [1] 1 ## [1] 3
try
try
在錯誤發生時,會傳回一個型態為try-error
的物件,讓使用者判斷有無發生錯誤r <- try(f(), silent = TRUE)
## [1] 1
class(r) # 可以藉此判斷錯誤有沒有發生
## [1] "try-error"
conditionMessage(attr(r, "condition")) # 可以取得呼叫`stop`時傳入的字串
## [1] "test error"
r <- try({}, silent = TRUE) class(r) # 不再是`try-error`
## [1] "NULL"
tryCatch
try
- catch
- finally
的錯誤處理機制。tryCatch({ # 進入 try-catch block 的程式碼 f() }, error = function(e) { # `e`就是捕捉到的錯誤(condition 物件) print(conditionMessage(e)) # 印出錯誤訊息 }, finally = { # 無論有沒有錯誤都一定會執行的程式碼 })
## [1] 1 ## [1] "test error"
C <- complex(real = rep(seq(-2.2, 1.0, length.out = dx), each = dy), imag = rep(seq(-1.2, 1.2, length.out = dy), dx))
each = dy
是哪個函數的參數?(不明顯,容易誤判). <- seq(-2.2, 1.0, length.out = dx) .real <- rep(., each = dy) . <- seq(-1.2, 1.2, length.out = dy) .imag <- rep(., dx) C <- complex(real = .real, imag = .imag)
browser()
,產生中斷點debug(f)
,在f
函數的開頭插入中斷點。可透過undebug
移除一開始的中斷點
f
可以是內建,或是套件提供的函數trace
可以暫時編輯一個函數的程式碼。(搭配browser
使用),請看現場demof <- function(a, ...) { browser() } f(1, b = 2, 3)
source("https://wush978.github.io/R/init-swirl.R")