You might have values containing decimals stored in a vector when working in R, and want to round all of the numbers to a certain number of places. Maybe you just want to display the data rounded a certain way, or need to use and manipulate the data. You can specify this using the round() function!
First, we’ll load a package and data set and extract a numeric column, and then create a vector of those values. By printing the vector, we can see that the values are given to 1 decimal place.
# install.packages('palmerpenguins')
library(palmerpenguins)
## Warning: package 'palmerpenguins' was built under R version 4.1.2
data(penguins)
# extract bill_depth_mm column, assign it to a vector
orig_vector <- c(penguins$bill_depth_mm)
orig_vector
## [1] 18.7 17.4 18.0 NA 19.3 20.6 17.8 19.6 18.1 20.2 17.1 17.3 17.6 21.2 21.1
## [16] 17.8 19.0 20.7 18.4 21.5 18.3 18.7 19.2 18.1 17.2 18.9 18.6 17.9 18.6 18.9
## [31] 16.7 18.1 17.8 18.9 17.0 21.1 20.0 18.5 19.3 19.1 18.0 18.4 18.5 19.7 16.9
## [46] 18.8 19.0 18.9 17.9 21.2 17.7 18.9 17.9 19.5 18.1 18.6 17.5 18.8 16.6 19.1
## [61] 16.9 21.1 17.0 18.2 17.1 18.0 16.2 19.1 16.6 19.4 19.0 18.4 17.2 18.9 17.5
## [76] 18.5 16.8 19.4 16.1 19.1 17.2 17.6 18.8 19.4 17.8 20.3 19.5 18.6 19.2 18.8
## [91] 18.0 18.1 17.1 18.1 17.3 18.9 18.6 18.5 16.1 18.5 17.9 20.0 16.0 20.0 18.6
## [106] 18.9 17.2 20.0 17.0 19.0 16.5 20.3 17.7 19.5 20.7 18.3 17.0 20.5 17.0 18.6
## [121] 17.2 19.8 17.0 18.5 15.9 19.0 17.6 18.3 17.1 18.0 17.9 19.2 18.5 18.5 17.6
## [136] 17.5 17.5 20.1 16.5 17.9 17.1 17.2 15.5 17.0 16.8 18.7 18.6 18.4 17.8 18.1
## [151] 17.1 18.5 13.2 16.3 14.1 15.2 14.5 13.5 14.6 15.3 13.4 15.4 13.7 16.1 13.7
## [166] 14.6 14.6 15.7 13.5 15.2 14.5 15.1 14.3 14.5 14.5 15.8 13.1 15.1 14.3 15.0
## [181] 14.3 15.3 15.3 14.2 14.5 17.0 14.8 16.3 13.7 17.3 13.6 15.7 13.7 16.0 13.7
## [196] 15.0 15.9 13.9 13.9 15.9 13.3 15.8 14.2 14.1 14.4 15.0 14.4 15.4 13.9 15.0
## [211] 14.5 15.3 13.8 14.9 13.9 15.7 14.2 16.8 14.4 16.2 14.2 15.0 15.0 15.6 15.6
## [226] 14.8 15.0 16.0 14.2 16.3 13.8 16.4 14.5 15.6 14.6 15.9 13.8 17.3 14.4 14.2
## [241] 14.0 17.0 15.0 17.1 14.5 16.1 14.7 15.7 15.8 14.6 14.4 16.5 15.0 17.0 15.5
## [256] 15.0 13.8 16.1 14.7 15.8 14.0 15.1 15.2 15.9 15.2 16.3 14.1 16.0 15.7 16.2
## [271] 13.7 NA 14.3 15.7 14.8 16.1 17.9 19.5 19.2 18.7 19.8 17.8 18.2 18.2 18.9
## [286] 19.9 17.8 20.3 17.3 18.1 17.1 19.6 20.0 17.8 18.6 18.2 17.3 17.5 16.6 19.4
## [301] 17.9 19.0 18.4 19.0 17.8 20.0 16.6 20.8 16.7 18.8 18.6 16.8 18.3 20.7 16.6
## [316] 19.9 19.5 17.5 19.1 17.0 17.9 18.5 17.9 19.6 18.7 17.3 16.4 19.0 17.3 19.7
## [331] 17.3 18.8 16.6 19.9 18.8 19.4 19.5 16.5 17.0 19.8 18.1 18.2 19.0 18.7
The round() function takes 2 arguments, the first being the name of the vector being rounded, and the second being the number of digits. The default setting of the round() function is 0 digits, so if we do not specify this and then display it we see that each value has 0 places after the decimal.
round(orig_vector)
## [1] 19 17 18 NA 19 21 18 20 18 20 17 17 18 21 21 18 19 21 18 22 18 19 19 18 17
## [26] 19 19 18 19 19 17 18 18 19 17 21 20 18 19 19 18 18 18 20 17 19 19 19 18 21
## [51] 18 19 18 20 18 19 18 19 17 19 17 21 17 18 17 18 16 19 17 19 19 18 17 19 18
## [76] 18 17 19 16 19 17 18 19 19 18 20 20 19 19 19 18 18 17 18 17 19 19 18 16 18
## [101] 18 20 16 20 19 19 17 20 17 19 16 20 18 20 21 18 17 20 17 19 17 20 17 18 16
## [126] 19 18 18 17 18 18 19 18 18 18 18 18 20 16 18 17 17 16 17 17 19 19 18 18 18
## [151] 17 18 13 16 14 15 14 14 15 15 13 15 14 16 14 15 15 16 14 15 14 15 14 14 14
## [176] 16 13 15 14 15 14 15 15 14 14 17 15 16 14 17 14 16 14 16 14 15 16 14 14 16
## [201] 13 16 14 14 14 15 14 15 14 15 14 15 14 15 14 16 14 17 14 16 14 15 15 16 16
## [226] 15 15 16 14 16 14 16 14 16 15 16 14 17 14 14 14 17 15 17 14 16 15 16 16 15
## [251] 14 16 15 17 16 15 14 16 15 16 14 15 15 16 15 16 14 16 16 16 14 NA 14 16 15
## [276] 16 18 20 19 19 20 18 18 18 19 20 18 20 17 18 17 20 20 18 19 18 17 18 17 19
## [301] 18 19 18 19 18 20 17 21 17 19 19 17 18 21 17 20 20 18 19 17 18 18 18 20 19
## [326] 17 16 19 17 20 17 19 17 20 19 19 20 16 17 20 18 18 19 19
However, we can also specify the number of digits using this argument, and we get the same result.
round(orig_vector, digits = 0)
## [1] 19 17 18 NA 19 21 18 20 18 20 17 17 18 21 21 18 19 21 18 22 18 19 19 18 17
## [26] 19 19 18 19 19 17 18 18 19 17 21 20 18 19 19 18 18 18 20 17 19 19 19 18 21
## [51] 18 19 18 20 18 19 18 19 17 19 17 21 17 18 17 18 16 19 17 19 19 18 17 19 18
## [76] 18 17 19 16 19 17 18 19 19 18 20 20 19 19 19 18 18 17 18 17 19 19 18 16 18
## [101] 18 20 16 20 19 19 17 20 17 19 16 20 18 20 21 18 17 20 17 19 17 20 17 18 16
## [126] 19 18 18 17 18 18 19 18 18 18 18 18 20 16 18 17 17 16 17 17 19 19 18 18 18
## [151] 17 18 13 16 14 15 14 14 15 15 13 15 14 16 14 15 15 16 14 15 14 15 14 14 14
## [176] 16 13 15 14 15 14 15 15 14 14 17 15 16 14 17 14 16 14 16 14 15 16 14 14 16
## [201] 13 16 14 14 14 15 14 15 14 15 14 15 14 15 14 16 14 17 14 16 14 15 15 16 16
## [226] 15 15 16 14 16 14 16 14 16 15 16 14 17 14 14 14 17 15 17 14 16 15 16 16 15
## [251] 14 16 15 17 16 15 14 16 15 16 14 15 15 16 15 16 14 16 16 16 14 NA 14 16 15
## [276] 16 18 20 19 19 20 18 18 18 19 20 18 20 17 18 17 20 20 18 19 18 17 18 17 19
## [301] 18 19 18 19 18 20 17 21 17 19 19 17 18 21 17 20 20 18 19 17 18 18 18 20 19
## [326] 17 16 19 17 20 17 19 17 20 19 19 20 16 17 20 18 18 19 19
Here’s another quick example to show the specification of digits after the decimal.
round(c(1.33,2.4,5.66, 5.765, 7.2), digits = 2)
## [1] 1.33 2.40 5.66 5.76 7.20
This link contains more helpful information about the round() function: https://www.datasciencemadesimple.com/round-function-in-r/