Here are five ways to round numbers in R:
Rounding is a process of approximating a number to a shorter, more straightforward, and interpretable value while retaining its closeness to the original value.
Here are some basic different types of rounding we use in our day-to-day life:
The round() function rounds numbers to the nearest integer or to a specified number of decimal places. For example, round(2.7) would give 3, and round(2.123, digits=1) would be 2.1.
You can use it in general-purpose rounding where minor edge-case differences are acceptable.
num_vec <- c(11.23, 46.56, -21.89, -12.14)
round(num_vec)
Output
[1] 11 47 -22 -12
The signif() function rounds the value to a specified number of significant digits. It means keeping the first few most significant digits and rounding off the rest.
For example, signif(123456, digits=3) would give 123000. This type of rounding is helpful when dealing with extremely large numbers, and maintaining a specific precision is desired.
num_vec <- c(11.23, 46.56, 21.89)
# round each number to one significant digit
signif(num_vec, 1)
Output
[1] 10 50 20
Here is the explanation of the output:
The ceiling() function rounds up the value to the nearest integer. It helps round up regardless of the decimal value. The output value is greater than or equal to the input value.
For example, ceiling(2.3) gives us 3. The real-life application for the ceiling is allocating space/resources.
num_vec <- c(11.23, 46.56, -21.89)
ceiling(num_vec)
Output
[1] 12 47 -21
Here is the explanation of the output:
The floor() function rounds down the value to the next nearest integer. It works opposite of the ceiling() function.
num_vec <- c(11.2313, 46.5633, -21.89)
floor(num_vec)
Output
[1] 11 46 -22
If you round down 11.2313, it will return 11, which is the nearest integer. For 46.5633, it will return 46, and for -21.89, it will return -22.
The trunc() function always truncates (cuts off) decimal places from values, effectively rounding towards zero. So, trunc(2.7) is 2 and trunc(-2.7) is -2. That’s different from the floor(), which would give -3 for -2.7.
num_vec <- c(19.21, 21.4619, -21.18)
trunc(num_vec)
Output
[1] 19 21 -21
The truncation of 19.21 is 19 because after removing .21, 19 is close to 0. So, the output is 19.
The truncation of 21.4619 is 21 because after removing .4619, 21 is close to 0. So, the output is 21.
The truncation of -21.18 is -21 because after removing .18, -21 is close to 0. So, the output is -21.
That’s it.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
The scale() function in R centers (subtracting the mean) and/or scales (dividing by the standard…
To rename a file in R, you can use the file.rename() function. It renames a…
The prop.table() function in R calculates the proportion or relative frequency of values in a…
The exp() is a built-in function that calculates the exponential of its input, raising Euler's…
The split() function divides the input data into groups based on some criteria, typically specified…
The colMeans() function in R calculates the arithmetic mean of columns in a numeric matrix,…
View Comments
This a great feature. Really Cool. Respect an opportunity to get super stats regarding units blogs and forums, too! I serious like everything about this wundabar artical...