開始使用免費開始

選用引數

來看看 round() 函式的說明文件。它會把數值向量四捨五入到指定的小數位數。

round(x, digits = 0)

第一個引數 x 是必要的。沒有它,函式不會運作!

引數 digits 稱為「選用引數」。選用引數不一定要由使用者設定,因為它可能有預設值,或是函式可以從你提供的其他資料推論出來。即使不必設定,它們常能提供額外的彈性。在這裡,digits 用來指定要四捨五入到第幾位小數。

在這個練習中探索 round() 吧!

本練習屬於課程

R 金融中級

檢視課程

練習說明

  • 5.4 使用 round()
  • 5.4 使用 round(),並指定 digits = 1
  • 已為你建立一個向量 numbers
  • numbers 使用 round(),並指定 digits = 3

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Round 5.4
___

# Round 5.4 with 1 decimal place
___

numbers <- c(.002623, pi, 812.33345)

# Round numbers to 3 decimal places
___
編輯並執行程式碼