round_any Function of plyr Package – Round to Multiple in R (Example Code)
In this R tutorial you’ll learn how to apply the round_any function of the plyr package.
Example Data
set.seed(873465) # Set random seed my_data <- rnorm(20) # Create vector object in R my_data # Display vector in RStudio console # [1] -2.7994931 0.6241767 0.4809905 1.0506628 0.1281959 0.2434197 # [7] -1.9444261 -1.5903495 1.0987325 0.4428026 -0.8621559 -0.2770290 # [13] -0.4233355 0.2341515 0.9399159 0.7113954 0.8803895 -1.0645866 # [19] 0.2936354 1.3757329 |
set.seed(873465) # Set random seed my_data <- rnorm(20) # Create vector object in R my_data # Display vector in RStudio console # [1] -2.7994931 0.6241767 0.4809905 1.0506628 0.1281959 0.2434197 # [7] -1.9444261 -1.5903495 1.0987325 0.4428026 -0.8621559 -0.2770290 # [13] -0.4233355 0.2341515 0.9399159 0.7113954 0.8803895 -1.0645866 # [19] 0.2936354 1.3757329
Example: How to Apply the round_any() Function of the plyr Package
install.packages("plyr") # Install plyr package library("plyr") # Load plyr |
install.packages("plyr") # Install plyr package library("plyr") # Load plyr
round_any(my_data, 0.5) # Using round_any() function # [1] -3.0 0.5 0.5 1.0 0.0 0.0 -2.0 -1.5 1.0 0.5 -1.0 -0.5 -0.5 0.0 1.0 # [16] 0.5 1.0 -1.0 0.5 1.5 |
round_any(my_data, 0.5) # Using round_any() function # [1] -3.0 0.5 0.5 1.0 0.0 0.0 -2.0 -1.5 1.0 0.5 -1.0 -0.5 -0.5 0.0 1.0 # [16] 0.5 1.0 -1.0 0.5 1.5