什么是函数?

R 金融中级

Lore Dirick

Manager of Data Science Curriculum at Flatiron School

函数示例

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

函数的组成

  • 参数
    • 输入/数据
    • 选项
  • 主体
    • 代码执行
  • 返回值
    • 稳定且可预测的输出
R 金融中级

函数文档

?matrix

2021-04-09 上午9.50.10 的屏幕截图.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 金融中级

Passons à la pratique !

R 金融中级

Preparing Video For Download...