How to round numbers in R (5 Examples)

Rounding numbers in R is easy to do with the round() function. This function rounds numbers to the nearest integer, or to a specified number of decimal places. For example, you can round a number to the nearest 10 or 100, or to a specific number of decimal places such as 0.1 or 0.01. You can also use the ceiling() and floor() functions to round numbers up or down to the nearest integer. Using these functions you can easily round numbers quickly and accurately in R.


You can use the following functions to round numbers in R:

  • round(x, digits = 0): Rounds values to specified number of decimal places.
  • signif(x, digits = 6): Rounds values to specified number of significant digits.
  • ceiling(x): Rounds values up to nearest integer.
  • floor(x): Rounds values down to nearest integer.
  • trunc(x): Truncates (cuts off) decimal places from values.

The following examples show how to use each of these functions in practice.

Example 1: round() Function in R

The following code shows how to use the round() function in R:

#define vector of data
data <- c(.3, 1.03, 2.67, 5, 8.91)

#round values to 1 decimal place
round(data, digits = 1)

[1] 0.3 1.0 2.7 5.0 8.9

Example 2: signif() Function in R

The following code shows how to use the signif() function to round values to a specific number of significant digits in R:

#define vector of data
data <- c(.3, 1.03, 2.67, 5, 8.91)

#round values to 3 significant digits
signif(data, digits = 3)

[1] 0.30 1.03 2.67 5.00 8.91

Example 3: ceiling() Function in R

The following code shows how to use the ceiling() function to round values up to the nearest integer:

#define vector of data
data <- c(.3, 1.03, 2.67, 5, 8.91)

#round values up to nearest integer
ceiling(data)

[1] 1 2 3 5 9

Example 4: floor() Function in R

The following code shows how to use the floor() function to round values down to the nearest integer:

#define vector of data
data <- c(.3, 1.03, 2.67, 5, 8.91)

#round values down to nearest integer
floor(data)

[1] 0 1 2 5 8

Example 5: trunc() Function in R

The following code shows how to use the trunc() function to truncate (cut off) decimal places from values:

#define vector of data
data <- c(.3, 1.03, 2.67, 5, 8.91)

#truncate decimal places from values
trunc(data)

[1] 0 1 2 5 8

x