logo

Rounding Up, Down, Off, and Changing Digits in R 📂R

Rounding Up, Down, Off, and Changing Digits in R

Overview

20180531\_232701.png

The ceiling() function rounds up, and the floor() function rounds down. These functions may seem unnecessary in R, which mainly deals with statistics, but they are surprisingly convenient when handling data.

Explanation

  • The trunc() function also discards everything below the decimal point, but it returns the value that is closer to $0$.
  • Both the round() function and the signif() function keep a number of digits, but they differ in that round() cares about the digits below the decimal point, while signif() cares about the whole number. Usually you would just use round(), but if you want the results to come out neatly aligned in columns, you need the signif() function.

Meanwhile, instead of adjusting numbers one by one with such functions, if you want to change the number of digits across the entire project, use the options() function. If you set the desired number of digits in the digits option, all real numbers are printed with the specified number of digits.