In practice, numbers are not always as well behaved as they are in theory. The problem exists because functions do not always produce easy to read numbers. Thus, it is often necessary to round numbers to make them more readable.
Dividing two numbers is an excellent example of the need for rounding since it often produces results with a lot of digits after the decimal point. Rounding a number can be done in several different ways and so most programming languages have several rounding functions. R is no different in this. When rounding a number, you can round it up or down. You can round it to the nearest whole number or a predetermined number of digits. So, several functions are needed for the task.
Function: Round in R
Round is an R rounding function with the format of round(number,digits) that rounds the number to the number of digits provided. Here is an example of the round function in action.
# round in R example > round(1234.56789,3)  1234.568 > round(1234.56789,0)  1235 > round(1234.56789,-3)  1000
A positive number of digits produce that number of digits after the decimal point up to three. However, a negative number of digits zeros out that number of digits before the decimal point.
Function: Signif in R
Signif is an R rounding function with the format of signif(number,digits) and it rounds the number to the number of digits provided. Here is an example of the Signif function in action.
# signif in R example > signif(1234.566789,4)  1235 > signif(1234.566789,7)  1234.567 > signif(1234.566789,1)  1000
The signif function round to the given number digits. Both the round and signif functions use standard rounding conventions.
Function: Floor in R
Floor is an R rounding function with the format of floor(number) and it rounds the number to the nearest integer that is less than its value. Here is an example of the floor function in action.
# floor in r example > floor(3.14159)  3 > floor(-3.14159)  -4
As you can see when you insert pi into the floor function, the result is 3, however, when you insert -pi you get -4. This is the way this function works and why it is called “floor.”
Function: Ceiling in R
Ceiling is an R rounding function with the format of ceiling(number) that rounds the number to the nearest integer that is greater than its value. Here is an example of the ceiling function in action.
# ceiling in R example > ceiling(3.14159)  4 > ceiling(-3.14159)  -3
As you can see when you insert pi into the ceiling function, the result is 4. On the other hand, when you insert -pi you get -3. This is the way this function works and why it is called “ceiling.”
Function: Trunc in R
Trunc is an R rounding function with the format of trunc(number) that rounds to the whole number part of the number. Here is an example of the trunc function in action.
#Trunc in R example > trunc(3.14159)  3 > trunc(-3.14159)  -3
Note that when you insert pi into the trunc function that you get 3 and that inserting -pi produces -3. This makes it the perfect function for obtaining the integer part of any number.
Rounding numbers has numerous applications simply because many functions do not produce nice neat results. R’s rounding functions do more than round individual numbers, for example, they can round the entire vector sequences. They also can round an entire R data frame in a single step. The main limit in rounding a complete R data frame is that the rounding functions to only work on numeric values. The solution to this problem is to use the cbind function on the numeric values first, round them, and then use the cbind function on the numeric portion with the text portion. Another limit is that the chart data needs to be complete in all columns or it will produce an error message. When used properly, these R rounding tools are very useful and easy to use.
Rounding numbers is an important part of presenting data meaningfully. In most cases, calculated values do not have any meaning beyond two or three digits so getting rid of the extra digits does not reduce actual precision.