What are some examples of rounding numbers in R?

What are some examples of rounding numbers in R?

Rounding numbers is a commonly used mathematical operation in the R programming language. It involves adjusting a number to the nearest whole number or a specified decimal place. Some examples of rounding numbers in R include using the round() function to round a number to the nearest integer, the floor() function to round a number down to the nearest integer, and the ceiling() function to round a number up to the nearest integer. Additionally, the signif() function can be used to round a number to a specified number of significant digits. Rounding numbers is essential in data analysis and can be applied in various scenarios such as financial calculations, data visualization, and statistical analysis.

Round Numbers in R (5 Examples)


You can use the following functions to round numbers in R:

  • round(x, digits = 0): Rounds values to specified number of decimal places.
  • signif(x, digits = 6): Rounds values to specified number of significant digits.
  • ceiling(x): Rounds values up to nearest integer.
  • floor(x): Rounds values down to nearest integer.
  • trunc(x): Truncates (cuts off) decimal places from values.

The following examples show how to use each of these functions in practice.

Example 1: round() Function in R

The following code shows how to use the round() function in R:

#define vector of data
data <- c(.3, 1.03, 2.67, 5, 8.91)

#round values to 1 decimal place
round(data, digits = 1)

[1] 0.3 1.0 2.7 5.0 8.9

Example 2: signif() Function in R

The following code shows how to use the signif() function to round values to a specific number of significant digits in R:

#define vector of data
data <- c(.3, 1.03, 2.67, 5, 8.91)

#round values to 3 significant digits
signif(data, digits = 3)

[1] 0.30 1.03 2.67 5.00 8.91

Example 3: ceiling() Function in R

The following code shows how to use the ceiling() function to round values up to the nearest integer:

#define vector of data
data <- c(.3, 1.03, 2.67, 5, 8.91)

#round values up to nearest integer
ceiling(data)

[1] 1 2 3 5 9

Example 4: floor() Function in R

The following code shows how to use the floor() function to round values down to the nearest integer:

#define vector of data
data <- c(.3, 1.03, 2.67, 5, 8.91)

#round values down to nearest integer
floor(data)

[1] 0 1 2 5 8

Example 5: trunc() Function in R

The following code shows how to use the trunc() function to truncate (cut off) decimal places from values:

#define vector of data
data <- c(.3, 1.03, 2.67, 5, 8.91)

#truncate decimal places from values
trunc(data)

[1] 0 1 2 5 8

Cite this article

stats writer (2024). What are some examples of rounding numbers in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/what-are-some-examples-of-rounding-numbers-in-r/

stats writer. "What are some examples of rounding numbers in R?." PSYCHOLOGICAL SCALES, 4 May. 2024, https://scales.arabpsychology.com/stats/what-are-some-examples-of-rounding-numbers-in-r/.

stats writer. "What are some examples of rounding numbers in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/what-are-some-examples-of-rounding-numbers-in-r/.

stats writer (2024) 'What are some examples of rounding numbers in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/what-are-some-examples-of-rounding-numbers-in-r/.

[1] stats writer, "What are some examples of rounding numbers in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. What are some examples of rounding numbers in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
PDF
Scroll to Top