round_any Function of plyr Package in R (2 Examples)

 

This tutorial demonstrates how to apply the round_any function of the plyr package in the R programming language.

The tutorial is structured as follows:

Let’s dive into it.

 

Exemplifying Data & Software Packages

We’ll use the following data as basement for this R tutorial:

set.seed(562981)                  # Create random vector
x <- rnorm(10)
x                                 # Print random vector
#  [1]  0.4727372 -0.7717060 -0.9445741  0.6140858  0.2909179  0.1659105
#  [7] -0.2909570  1.1854541 -0.4401599  0.3069997

Have a look at the previous output of the RStudio console. It shows that our example data contains ten randomly distributed numeric values.

We also have to install and load the plyr package (note that we are talking about the plyr, not the dplyr package), if we want to use the corresponding commands and functions:

install.packages("plyr")          # Install & load plyr
library("plyr")

We are set up! Let’s move on to the application of the round_any function in R.

 

Example 1: Round with Accuracy of 1 Using round_any() Function of plyr Package

Generally speaking: The round_any function rounds to a multiple of any number that we specify within the round_any function.

This example illustrates how to round to the closest multiplier of 1 (in other words: an accuracy of 1) using the round_any function.

For this, we have to specify the name of our data object (i.e. x) as well as the accuracy (i.e. 1):

x_round1 <- round_any(x, 1)       # Apply round_any
x_round1                          # Print rounded vector
#  [1]  0 -1 -1  1  0  0  0  1  0  0

The previous output shows that we have created a new data object called x_round1, where the values in our input vector x have been rounded to the closest multiplier of the value 1.

 

Example 2: Round with Accuracy of 0.25 Using round_any() Function of plyr Package

The round_any function can be used to round to basically any multiplier you want.

Example 2 illustrates how to round to the closest multiplier of 0.25:

x_round2 <- round_any(x, 0.25)    # Apply round_any
x_round2                          # Print rounded vector
#  [1]  0.50 -0.75 -1.00  0.50  0.25  0.25 -0.25  1.25 -0.50  0.25

Note that the examples of this tutorial have used the round function to round our data, i.e. the default rounding function of round_any. However, you may change the f argument of the round_any function to floor or ceiling, in case you prefer to use another rounding function instead.

 

Video, Further Resources & Summary

Would you like to learn more about the application of the round_any function of the plyr package? Then you may want to have a look at the following video on my YouTube channel. In the video, I demonstrate the R codes of this tutorial in RStudio.

 

 

Furthermore, you could read the related articles on my website.

 

Summary: You have learned in this article how to use the round_any function in the R programming language. Let me know in the comments section, in case you have further questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


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.

Top