There are several ways to round up a number in r programming. In most cases, whether the function rounds up or down depends upon the number. However, there are functions by which you can ensure the number will always be rounded up. With so many rounding options, you are likely to find one that will produce the results you are looking for.
Options to Round Numbers in R
When you round up a number, you have four functions to choose from. The round function has the format of round(x) and it rounds the numeric variable x to the nearest integer. The signif function has the format of signif(x, digits) and it rounds the numeric variable x to the indicated significant digit. The ceiling function with the format of the ceiling(x) and will round the numeric variable x to the next integer. The round_any function has the format of round_any(x,n,f) and it rounds the numeric variable x to the nearest multiple of n and f allows you to set the function to round either up or down.
How it Works – Rounding in R
When you use the round function, the function removes the digits to the right of the decimal place producing an integer value. It is similar to the trunc function, except that it will round up when the digit right after the decimal point is greater than four. However, if you use the ceiling function it will always round up. This is the key to ensuring that you will always get your number to be rounded up, otherwise, it might end up being rounded down. If you do not want this possibility, the ceiling function is what you are going to need.
Examples of Ways to Round Up in R
Here we have several examples of round up a number. They include a negative number to illustrate the handling of negative numbers.
> x = c(1.7, 2.8, 3.6, 11.8, -55.4, 110.9, 776.8)
> round(x)
[1] 2 3 4 12 -55 111 777
In this example, we used the round function to round each number to the nearest integer.
> x = c(1.7, 2.8, 3.6, 18.2, -54.4, 170.9, 776.8)
> signif(x, digits = 1)
[1] 2 3 4 20 -50 200 800
In this example, we used the signif function to round each number to one significant digit. We could have selected any number of significant digits we wanted.
> x = c(1.7, 2.3, 3.4, 11.8, -55.4, 110.4, 776.8)
> ceiling(x)
[1] 2 3 4 12 -55 111 777
In this example, we used the ceiling function to round each number to the next integer. This always involves rounding up.
> library(“plyr”)
> x = c(1.7, 2.3, 3.4, 11.8, -55.4, 110.4, 776.8)
> round_any(x, 10, f = ceiling)
[1] 10 10 10 20 -50 120 780
In this example, we used the round_any function along with the ceiling function to round each number to the next nearest multiple of ten. This always involves rounding up.
Application: When to Round Up in R
The applications of rounding numbers up include a rounded sum so that the value has nothing to the right of the decimal. Rounding numbers can come in handy when you do not have room in a display for numbers after the decimal point. It can also come in handy when graphing data.
Rounding up numbers is a fairly common practice of data manipulation; it helps to eliminate some of the roughness. With four different functions to round up numbers, you can safely and easily get the effect that you are looking for. All four functions are easy to use, and once you understand how to use them you can create a variety of rounding effects.