函式入門

R 中級

Filip Schouwenaars

DataCamp Instructor

函式

  • 你早就用過了!
  • 建立清單:list()
  • 顯示變數:print()
R 中級

黑箱原理

R 中級

黑箱原理

R 中級

黑箱原理

R 中級

黑箱原理

R 中級

黑箱原理

R 中級

黑箱原理

R 中級

黑箱原理

R 中級

在 R 中呼叫函式

sd(c(1, 5, 6, 7))
2.629956
values <- c(1, 5, 6, 7)
sd(values)
2.629956
my_sd <- sd(values)
my_sd
2.629956
R 中級

函式說明文件

help(sd)
?sd
sd(x, na.rm = FALSE)

Screen Shot 2021-04-09 at 9.22.28 AM.png

R 中級

問題

sd(x, na.rm = FALSE)
  • 參數名稱:x、na.rm
  • na.rm = FALSE
  • sd(values) 可行嗎?
R 中級

參數比對

sd(x, na.rm = FALSE)

依位置

sd(values)

依名稱

sd(x = values)
R 中級

na.rm 參數

values <- c(1, 5, 6, NA)
sd(values)
NA
sd(x, na.rm = FALSE)
sd(values, TRUE)
2.645751
sd(values, na.rm = TRUE)
2.645751
R 中級

sd(values) 可行嗎?

values <- c(1, 5, 6, 7)
sd(values)
2.629956
sd()
Error in is.data.frame(x) : 
argument "x" is missing, with no default
sd(x, na.rm = FALSE)
R 中級

實用小技巧

args(sd)
function (x, na.rm = FALSE) 
NULL
R 中級

總結

  • 函式如同黑箱
  • 參數比對:依位置或依名稱
  • 參數可以有預設值
R 中級

一起來練習吧!

R 中級

Preparing Video For Download...