How to Round Numbers in R

Here are five ways to round numbers in R:

  1. round(): It rounds values to a specified number of decimal places.
  2. signif(): It rounds values to several significant digits.
  3. ceiling(): It rounds values up to the nearest integer.
  4. floor(): It rounds values down to the nearest integer.
  5. trunc(): It truncates (cuts off) decimal places from values.

Method 1: Using round(x, digits)

Figure of using the round(x, digits) function to round numbers in R

num_vec <- c(11.23, 46.56, 21.89)

# round each number to one decimal place
round(num_vec, 1)

Output

[1] 11.2 46.6 21.9

Method 2: Using signif()

Rounding to a specified number of significant digits means keeping the first few most significant digits and rounding off the rest.

Diagram of using the signif(x, digits) function to round numbersi in R

num_vec <- c(11.23, 46.56, 21.89)

# round each number to one significant digit
signif(num_vec, 1)

Output

[1] 10 50 20

Here is the explanation of the output:

  1. 11.23 is rounded to 10 because the first significant digit is 1.
  2. 46.56 is rounded to 50 because the first significant digit is 4.
  3. 21.89 is rounded to 20 because the first significant digit is 2.

Method 3: Using ceiling()

Diagram of using the ceiling(x) function to round numbers in R

num_vec <- c(11.23, 46.56, 21.89)

ceiling(num_vec)

Output

[1] 12 47 22

Here is the explanation of the output:

  1. ceiling(11.23) rounded from 11.23 up to 12.
  2. ceiling(46.56) rounded from 46.56 up to 47.
  3. ceiling(21.89) rounded from 21.89 up to 22.

Method 4: Using floor()

Diagram of using the floor(x) function to round numbers in R

num_vec <- c(11.2313, 46.5633, -21.89)

floor(num_vec)

Output

[1] 11 46 -22

Method 5: Using trunc()

Figure of using the trunc(x) function to round numbers in R

num_vec <- c(19.21, 21.4619, -21.18)

trunc(num_vec)

Output

[1] 19 21 -21

That’s it.

1 thought on “How to Round Numbers in R”

  1. This a great feature. Really Cool. Respect an opportunity to get super stats regarding units blogs and forums, too! I serious like everything about this wundabar artical…

    Reply

Leave a Comment