How to Round Numbers in R (5 Examples)


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

Additional Resources

How to Transform Data in R (Log, Square Root, Cube Root)
How to Perform an Arcsine Transformation in R
How to Find the Antilog of Values in R

Leave a Reply

Your email address will not be published.