什麼是函式?

R 金融中級

Lore Dirick

Manager of Data Science Curriculum at Flatiron School

函式範例

  • mean()
  • plot()
  • ncol()
R 金融中級

函式的組成

  • 參數
    • 輸入/資料
    • 選項
  • 主體
    • 執行程式碼
  • 回傳
    • 穩定可預期的輸出
R 金融中級

函式說明文件

?matrix

螢幕截圖 2021-04-09 9.50.10 AM.png

R 金融中級

函式參數

  • 必填
    • 缺少會拋出錯誤
    • 通常是資料/物件
  • 選填
    • 有預設值
    • 通常用來設定額外選項
R 金融中級

函式參數範例

returns <- c(.023, .044, .034, NA)

mean()
Error in mean.default() : argument "x" is missing, with no default
mean(returns)
NA
?mean

ch4_vid1_slides.021.png

mean(returns, na.rm = TRUE)
0.03366667
R 金融中級

一起來練習吧!

R 金融中級

Preparing Video For Download...