Round Up to Nearest 10 or 100 in R (Example)


In this post, I’ll illustrate how to round up to the closest 10 or 100 in R programming.

The tutorial will consist of one example for the rounding of data. To be more specific, the tutorial consists of these contents:

Let’s get started!


Example Data

As first step, let’s define some example data in R:

x <- c(1, 5, 0.1, 111, 99.9)      # Example data
x                                 # Print example data
# 1.0   5.0   0.1 111.0  99.9

Have a look at the previously shown output of the RStudio console. It shows that our example data is a numeric vector containing five different values. Let’s round these data!


Example: Rounding Up to Nearest 10 (or Other Values) Using dplyr Package

In this Example, I’ll illustrate how to round numbers up to a specific higher value. In this example, we are using the round_any function of the dplyr package. Therefore, we first have to install and load the dplyr package, if we want to use the functions that are included in the package.

install.packages("dplyr")         # Install dplyr package
library("dplyr")                  # Load dplyr

The round_any function can be applied as shown below:

round_any(x, 10)                  # Rounds up or down
# 0   0   0 110 100

However, as you can see some of the values of our example vector are rounded down. If we want to round up to a specific multiplier, we have to specify the f argument to be equal to ceiling:

round_any(x, 10, f = ceiling)     # Rounds up to next 10
# 10  10  10 120 100

Compare the RStudio console outputs with and without the f argument. In case of f = ceiling, all numbers are rounded upwards.

In the previous two R codes, we specified to round to the next 10. However, the round_any function also allows to round to other values. For example, we can round to the next 100…

round_any(x, 100, f = ceiling)    # Rounds up to next 100
# 100 100 100 200 100

…or to an odd number such as 3:

round_any(x, 3, f = ceiling)      # Rounds up to next 3
# 3   6   3 111 102


Video, Further Resources & Summary

If you need more explanations on the R programming code of this tutorial, I can recommend to have a look at the following video of my YouTube channel. In the video, I’m illustrating the R codes of this article:


The YouTube video will be added soon.


Furthermore, you might want to read the related tutorials of my website.


Summary: This article illustrated how to round up to the nearest 10 or 100 in the R programming language. Please let me know in the comments section below, if you have additional questions or comments.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.