R Basic

How to Round Numbers in R

Rounding is a process of approximating a number to a shorter, simpler, and more interpretable value while retaining its closeness to the original value. For example, if you are working in the finance division, you often publish a research report on the economy, where numbers in reports often round to 2 decimal points.

Here are some basic different types of rounding we use in our day-to-day life:

  1. Decimal places: E.g., 3.141593.14 (2 decimals).
  2. Significant figures: E.g., 0.004560.0046 (2 significant figures).
  3. Integer values: E.g., 4.85.

Here are five ways to round numbers in R:

  1. Using round()
  2. Using signif()
  3. Using ceiling()
  4. Using floor()
  5. Using trunc()

Method 1: Using round()

The round() function rounds numbers to the nearest integer or specified decimal places. For example, round(2.7) would give 3, and round(2.123, digits=1) would be 2.1. You can use it in general-purpose rounding where minor edge-case differences are acceptable.

num_vec <- c(11.23, 46.56, -21.89, -12.14)

round(num_vec)

Output

[1]  11  47  -22  -12

Pros

  1. It provides a flexible rounding option.
  2. It follows the round-half-to-even rule (avoids systematic bias in large datasets).

Cons

  1. Its behavior is still unpredictable and can surprise the users.

Method 2: Using signif()

The signif() function rounds the value to a specified number of significant digits. It means keeping the first few most significant digits and rounding off the rest.

For example, signif(123456, digits=3) would give 123000. This type of rounding is helpful when you are dealing with a very large number and want to maintain a certain precision.

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 10 is the nearest significant digit to 11.23 and not 20.
  2. 46.56 is rounded to 50 because 50 is the nearest significant digit to 46.56 and not 40.
  3. 21.89 is rounded to 20 because 20 is the nearest significant digit to 21.89 and not 30.

Pros

  1. It preserves scientific notation.
  2. It is ideal for maintaining precision in research/scientific reports.

Cons

  1. It is not intuitive for decimal-place rounding.
  2. It may obscure small differences in large numbers.

Method 3: Using ceiling()

The ceiling() function rounds ​​up the value to the nearest integer. It is helpful in rounding up regardless of the decimal value. The output value >= the input value.

For example, ceiling(2.3) gives us 3. The real-life application for the ceiling is allocating space/resources.

num_vec <- c(11.23, 46.56, -21.89)

ceiling(num_vec)

Output

[1] 12 47 -21

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 -21.

Pros

  1. It ensures that values are not underestimated.

Cons

  1. In some instances, it overestimates values which is not desirable.

Method 4: Using floor()

The floor() function rounds ​​down the value to the next nearest integer. It works opposite of the ceiling() function.

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

floor(num_vec)

Output

[1] 11 46 -22

If you round down 11.2313, it will return 11, which is the nearest integer. For 46.5633, it will return 46, and For -21.89, it will return -22.

Pros

  1. It is good for conservative estimates.
  2. It works consistently across positive and negative numbers.

Cons

  1. Not suitable for neutralizing the behavior.
  2. It may underestimate the value intentionally.

Method 5: Using trunc()

The trunc() function always truncates (cuts off) decimal places from values, effectively rounding towards zero. So, trunc(2.7) is 2 and trunc(-2.7) is -2. That’s different from the floor(), which would give -3 for -2.7.

 

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

trunc(num_vec)

Output

[1] 19 21 -21

The truncation of 19.21 is 19 because after removing .21, 19 is near to 0. So, the output is 19.

The truncation of 21.4619 is 21 because after removing .4619, 21 is near to 0. So, the output is 21.

The truncation of -21.18 is -21 because after removing .18, -21 is near to 0. So, the output is -21.

Pros

  1. It is fast and predictable.

Cons

  1. It is not suitable for rounding because it directly removes the decimal without rounding.

That’s it.

View Comments

  • 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...

Recent Posts

How to Create an Empty Vector and Append Values in R

R vectors are atomic, which means they have homogeneous data types. They are contiguous in…

1 hour ago

How to Remove Single and Multiple Columns from Data Frame in R

DataFrames are like tables that contain rows and columns. Each column can have a different…

19 hours ago

How to Convert Date to Numeric in R

Dates in R are stored as the number of days since 1970-01-01, so converting a…

2 days ago

How to Create a Data Frame from Vectors in R

In R, you can think of a vector as a series of values in a…

2 weeks ago

R dplyr::filter() Function: Complete Guide

The dplyr filter() function in R subsets a data frame and retains all rows that…

2 weeks ago

R distinct() Function from dplyr

The dplyr::distinct() function in R removes duplicate rows from a data frame or tibble and keeps…

2 weeks ago