R: Round a number – Snippet #7

Discover how to round a number with R

Packages

This snippet requires the built-in package base. As it is automatically loaded by R, it does not need to be loaded manually.

Code

To round a number, use round().

round(54.87)

The code above outputs 55 in the console.

Console

Dive deeper

Round number stored in variable

It is possible to round a number stored in a variable.

x <- 54.87
round(x)

The code above outputs 55 in the console.

Console

Specify the number of digits

To specify the number of digits to keep, use digits. If digits is not specified, round() defaults to 0 digits.

round(
  54.87,
  digits = 1
)

The code above outputs 54.9 in the console.

Console

Resources

R: Rounding of Numbers