diff --git a/_freeze/posts/2024-12-31/index/execute-results/html.json b/_freeze/posts/2024-12-31/index/execute-results/html.json new file mode 100644 index 00000000..4fd692a0 --- /dev/null +++ b/_freeze/posts/2024-12-31/index/execute-results/html.json @@ -0,0 +1,15 @@ +{ + "hash": "f4468257f83b7eb0597964f4019f0956", + "result": { + "engine": "knitr", + "markdown": "---\ntitle: \"Rounding Numbers in R with Examples: A Comprehensive Guide\"\nauthor: \"Steven P. Sanderson II, MPH\"\ndate: \"2024-12-31\"\ncategories: [code, rtip]\ntoc: TRUE\ndescription: \"Master rounding numbers in R with this detailed guide. Learn how to use `round()`, `signif()`, `ceiling()`, `floor()`, and `trunc()` functions with practical examples.\"\nkeywords: [Programming, Rounding Numbers in R, R Programming Functions, Rounding Functions, Data Analysis in R, Numerical Precision in R, round() function in R, signif() function in R, ceiling() function in R, floor() function in R, trunc() function in R, How to round numbers in R programming, Examples of rounding functions in R, Using ceiling and floor functions in R, Understanding significant digits in R, Best practices for rounding in data analysis with R, R rounding functions, R programming rounding examples, How to round numbers in R, R round() function tutorial, R signif() function examples, R ceiling() vs floor(), R truncation examples, Rounding decimals in R, Rounding integers in R, Rounding techniques in R programming, Rounding for data analysis in R, Rounding edge cases in R, Rounding performance in R, Rounding in R data frames, Rounding for, financial calculations in R]\ndraft: TRUE\n---\n\n\n\n# Introduction\n\nRounding numbers is a fundamental operation in data analysis and scientific computing. Whether you're working with financial data, scientific measurements, or large datasets, rounding ensures precision and simplifies results. In R, several functions are available to handle rounding, each tailored to specific needs. This guide will walk you through the most commonly used rounding functions in R—`round()`, `signif()`, `ceiling()`, `floor()`, and `trunc()`—with practical examples and real-world applications.\n\n# Understanding Rounding in R\n\nRounding is the process of reducing the number of digits in a number while keeping its value close to the original. In R, rounding is essential for:\n\n- Simplifying numerical outputs.\n- Managing precision in calculations.\n- Preparing data for visualization or reporting.\n\n# Overview of Rounding Functions in R\n\nR provides five primary functions for rounding:\n\n1. **`round(x, digits = 0)`**: Rounds to the nearest value with a specified number of decimal places.\n2. **`signif(x, digits = 6)`**: Rounds to a specified number of significant digits.\n3. **`ceiling(x)`**: Rounds up to the nearest integer.\n4. **`floor(x)`**: Rounds down to the nearest integer.\n5. **`trunc(x)`**: Truncates the decimal part, effectively rounding towards zero.\n\nEach function serves a unique purpose, making it crucial to choose the right one for your task.\n\n# Using the `round()` Function\n\n## **Syntax**\n\n```R\nround(x, digits = 0)\n```\n- `x`: Numeric vector to be rounded.\n- `digits`: Number of decimal places to round to (default is 0).\n\n## **Examples**\n\n1. **Rounding to the nearest integer**:\n\n\n\n ::: {.cell}\n \n ```{.r .cell-code}\n round(3.14159) # Output: 3\n ```\n \n ::: {.cell-output .cell-output-stdout}\n \n ```\n [1] 3\n ```\n \n \n :::\n :::\n\n\n2. **Rounding to specific decimal places**:\n \n\n\n ::: {.cell}\n \n ```{.r .cell-code}\n round(3.14159, digits = 2) # Output: 3.14\n ```\n \n ::: {.cell-output .cell-output-stdout}\n \n ```\n [1] 3.14\n ```\n \n \n :::\n :::\n\n\n\n## **Use Cases**\n\n- Financial calculations (e.g., rounding currency values).\n- Simplifying outputs for reports.\n\n# Using the `signif()` Function\n\n## **Syntax**\n\n```R\nsignif(x, digits = 6)\n```\n- `x`: Numeric vector to be rounded.\n- `digits`: Number of significant digits.\n\n## **Examples**\n\n1. **Rounding to significant digits**:\n\n\n\n ::: {.cell}\n \n ```{.r .cell-code}\n signif(12345.6789, digits = 3) # Output: 12300\n ```\n \n ::: {.cell-output .cell-output-stdout}\n \n ```\n [1] 12300\n ```\n \n \n :::\n :::\n\n\n2. **Handling scientific notation**:\n \n\n\n ::: {.cell}\n \n ```{.r .cell-code}\n signif(0.000123456, digits = 2) # Output: 0.00012\n ```\n \n ::: {.cell-output .cell-output-stdout}\n \n ```\n [1] 0.00012\n ```\n \n \n :::\n :::\n\n\n\n## **Use Cases**\n\n- Scientific computations requiring precision.\n- Formatting numbers for publication.\n\n# Using the `ceiling()` Function\n\n## **Syntax**\n\n```R\nceiling(x)\n```\n- `x`: Numeric vector to be rounded up.\n\n## **Examples**\n\n1. **Rounding up positive numbers**:\n\n\n\n ::: {.cell}\n \n ```{.r .cell-code}\n ceiling(2.3) # Output: 3\n ```\n \n ::: {.cell-output .cell-output-stdout}\n \n ```\n [1] 3\n ```\n \n \n :::\n :::\n\n\n2. **Rounding up negative numbers**:\n \n\n\n ::: {.cell}\n \n ```{.r .cell-code}\n ceiling(-2.3) # Output: -2\n ```\n \n ::: {.cell-output .cell-output-stdout}\n \n ```\n [1] -2\n ```\n \n \n :::\n :::\n\n\n\n## **Use Cases**\n\n- Calculating minimum required resources (e.g., rounding up to the nearest whole unit).\n- Ensuring non-negative results in computations.\n\n# Using the `floor()` Function\n\n## **Syntax**\n\n```R\nfloor(x)\n```\n- `x`: Numeric vector to be rounded down.\n\n## **Examples**\n\n1. **Rounding down positive numbers**:\n\n\n\n ::: {.cell}\n \n ```{.r .cell-code}\n floor(2.7) # Output: 2\n ```\n \n ::: {.cell-output .cell-output-stdout}\n \n ```\n [1] 2\n ```\n \n \n :::\n :::\n\n\n2. **Rounding down negative numbers**:\n \n\n\n ::: {.cell}\n \n ```{.r .cell-code}\n floor(-2.7) # Output: -3\n ```\n \n ::: {.cell-output .cell-output-stdout}\n \n ```\n [1] -3\n ```\n \n \n :::\n :::\n\n\n\n## **Use Cases**\n\n- Allocating resources conservatively.\n- Data processing tasks requiring downward rounding.\n\n# Using the `trunc()` Function\n\n## **Syntax**\n\n```R\ntrunc(x)\n```\n- `x`: Numeric vector to be truncated.\n\n## **Examples**\n\n1. **Truncating positive numbers**:\n \n\n\n ::: {.cell}\n \n ```{.r .cell-code}\n trunc(3.9) # Output: 3\n ```\n \n ::: {.cell-output .cell-output-stdout}\n \n ```\n [1] 3\n ```\n \n \n :::\n :::\n\n\n2. **Truncating negative numbers**:\n \n\n\n ::: {.cell}\n \n ```{.r .cell-code}\n trunc(-3.9) # Output: -3\n ```\n \n ::: {.cell-output .cell-output-stdout}\n \n ```\n [1] -3\n ```\n \n \n :::\n :::\n\n\n\n## **Use Cases**\n\n- Financial calculations where fractional values are ignored.\n- Simplifying data for integer-based operations.\n\n# Comparing Rounding Functions\n\n| Function | Behavior | Example Input | Example Output |\n|------------|-----------------------------------|---------------|----------------|\n| `round()` | Rounds to nearest value | 2.5 | 2 |\n| `signif()` | Rounds to significant digits | 12345.6789 | 12300 |\n| `ceiling()`| Rounds up to nearest integer | 2.3 | 3 |\n| `floor()` | Rounds down to nearest integer | 2.7 | 2 |\n| `trunc()` | Truncates decimal places | -3.9 | -3 |\n\n# Practical Examples of Rounding in R\n\n## **Example: Financial Data**\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nprices <- c(19.99, 24.49, 5.75)\nround(prices, digits = 1) # Output: 20.0, 24.5, 5.8\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 20.0 24.5 5.8\n```\n\n\n:::\n:::\n\n\n\n## **Example: Scientific Computations**\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nvalues <- c(0.000123456, 12345.6789)\nsignif(values, digits = 3) # Output: 0.000123, 12300\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 1.23e-04 1.23e+04\n```\n\n\n:::\n:::\n\n\n\n## **Example: Data Visualization**\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\ndata <- c(2.3, 3.7, 4.1)\nceiling(data) # Output: 3, 4, 5\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 3 4 5\n```\n\n\n:::\n:::\n\n\n\n# Common Pitfalls and How to Avoid Them\n\n1. **Misunderstanding \"round to even\"**:\n - `round(0.5)` results in 0, not 1.\n2. **Floating-point precision issues**:\n - Use `all.equal()` to compare floating-point numbers.\n3. **Inconsistent rounding across platforms**:\n - Be aware of differences between R and other software like MATLAB.\n\n# Advanced Rounding Techniques\n\n1. **Combining Functions**:\n\n\n\n ::: {.cell}\n \n ```{.r .cell-code}\n x <- c(3.14159, 2.71828)\n round(floor(x), digits = 1) # Output: 3.0, 2.0\n ```\n \n ::: {.cell-output .cell-output-stdout}\n \n ```\n [1] 3 2\n ```\n \n \n :::\n :::\n\n\n2. **Rounding in Data Frames**:\n\n\n\n ::: {.cell}\n \n ```{.r .cell-code}\n df <- data.frame(a = c(1.234, 5.678), b = c(9.876, 3.210))\n df[] <- lapply(df, round, digits = 2)\n ```\n :::\n\n\n# Your Turn!\n\n**Problem**: Round the following vector using all five functions:\n\n```r\nx <- c(3.14159, 2.71828, 1.61803, -1.41421, -2.23607)\n```\n
Click here for Solution!\n**Solution**:\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nx <- c(3.14159, 2.71828, 1.61803, -1.41421, -2.23607)\n\ndata.frame(\n Original = x,\n Rounded = round(x, digits = 2),\n Significant = signif(x, digits = 3),\n Ceiled = ceiling(x),\n Floored = floor(x),\n Truncated = trunc(x)\n)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n Original Rounded Significant Ceiled Floored Truncated\n1 3.14159 3.14 3.14 4 3 3\n2 2.71828 2.72 2.72 3 2 2\n3 1.61803 1.62 1.62 2 1 1\n4 -1.41421 -1.41 -1.41 -1 -2 -1\n5 -2.23607 -2.24 -2.24 -2 -3 -2\n```\n\n\n:::\n:::\n\n\n
\n\n# Quick Takeaways\n\n- Rounding functions in R include `round()`, `signif()`, `ceiling()`, `floor()`, and `trunc()`.\n- Each function has unique use cases, from financial calculations to scientific precision.\n- Understanding their behavior ensures accurate and efficient data analysis.\n\n# Conclusion\n\nRounding numbers in R is a versatile and essential skill for programmers. By mastering these functions, you can handle a wide range of tasks with precision and confidence. Start applying these techniques in your projects today!\n\n# FAQs\n\n1. **What is the difference between `round()` and `signif()`?** \n `round()` focuses on decimal places, while `signif()` targets significant digits.\n\n2. **How does `ceiling()` handle negative numbers?** \n It rounds up towards zero.\n\n3. **Can I use rounding functions on data frames?** \n Yes, use `apply()` or `dplyr` for efficient operations.\n\n4. **What happens when rounding 0.5 in R?** \n R uses \"round to even,\" so 0.5 rounds to 0.\n\n5. **Are there alternatives to these functions?\n\n# References\n\n1. **R Documentation: `round()`** \n Official documentation for the `round()` function in R, detailing its syntax, parameters, and behavior. Available at: [https://www.rdocumentation.org/packages/base/versions/latest/topics/round](https://www.rdocumentation.org/packages/base/versions/latest/topics/round)\n\n2. **R Documentation: `signif()`** \n Official documentation for the `signif()` function in R, explaining its usage for rounding to significant digits. Available at: [https://www.rdocumentation.org/packages/base/versions/latest/topics/signif](https://www.rdocumentation.org/packages/base/versions/latest/topics/signif)\n\n3. **R Documentation: `ceiling()`** \n Official documentation for the `ceiling()` function in R, describing its functionality for rounding up to the nearest integer. Available at: [https://www.rdocumentation.org/packages/base/versions/latest/topics/ceiling](https://www.rdocumentation.org/packages/base/versions/latest/topics/ceiling)\n\n4. **R Documentation: `floor()`** \n Official documentation for the `floor()` function in R, which rounds down to the nearest integer. Available at: [https://www.rdocumentation.org/packages/base/versions/latest/topics/floor](https://www.rdocumentation.org/packages/base/versions/latest/topics/floor)\n\n5. **R Documentation: `trunc()`** \n Official documentation for the `trunc()` function in R, explaining its use for truncating decimal places. Available at: [https://www.rdocumentation.org/packages/base/versions/latest/topics/trunc](https://www.rdocumentation.org/packages/base/versions/latest/topics/trunc)\n\n------------------------------------------------------------------------\n\nHappy Coding! 🚀\n\n![Rounding in R](todays_post.png)\n\n------------------------------------------------------------------------\n\n*You can connect with me at any one of the below*:\n\n*Telegram Channel here*: \n\n*LinkedIn Network here*: \n\n*Mastadon Social here*: [https://mstdn.social/\\@stevensanderson](https://mstdn.social/@stevensanderson)\n\n*RStats Network here*: [https://rstats.me/\\@spsanderson](https://rstats.me/@spsanderson)\n\n*GitHub Network here*: \n\n*Bluesky Network here*: \n\n*My Book: Extending Excel with Python and R* here: \n\n------------------------------------------------------------------------\n\n\n\n```{=html}\n\n```\n", + "supporting": [], + "filters": [ + "rmarkdown/pagebreak.lua" + ], + "includes": {}, + "engineDependencies": {}, + "preserve": {}, + "postProcess": true + } +} \ No newline at end of file diff --git a/docs/index.html b/docs/index.html index 7b0a7aea..63205223 100644 --- a/docs/index.html +++ b/docs/index.html @@ -244,7 +244,7 @@
Categories
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+

 
diff --git a/docs/posts/2024-12-31/index.html b/docs/posts/2024-12-31/index.html new file mode 100644 index 00000000..daf56a43 --- /dev/null +++ b/docs/posts/2024-12-31/index.html @@ -0,0 +1,1292 @@ + + + + + + + + + + + + + +Rounding Numbers in R with Examples: A Comprehensive Guide – Steve’s Data Tips and Tricks + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
Draft
+
+
+ +
+
+
+

Rounding Numbers in R with Examples: A Comprehensive Guide

+
+
+ Master rounding numbers in R with this detailed guide. Learn how to use round(), signif(), ceiling(), floor(), and trunc() functions with practical examples. +
+
+
+
code
+
rtip
+
+
+
+ + +
+ +
+
Author
+
+

Steven P. Sanderson II, MPH

+
+
+ +
+
Published
+
+

December 31, 2024

+
+
+ + +
+ + +
+
+
Keywords
+

Programming, Rounding Numbers in R, R Programming Functions, Rounding Functions, Data Analysis in R, Numerical Precision in R, round() function in R, signif() function in R, ceiling() function in R, floor() function in R, trunc() function in R, How to round numbers in R programming, Examples of rounding functions in R, Using ceiling and floor functions in R, Understanding significant digits in R, Best practices for rounding in data analysis with R, R rounding functions, R programming rounding examples, How to round numbers in R, R round() function tutorial, R signif() function examples, R ceiling() vs floor(), R truncation examples, Rounding decimals in R, Rounding integers in R, Rounding techniques in R programming, Rounding for data analysis in R, Rounding edge cases in R, Rounding performance in R, Rounding in R data frames, Rounding for, financial calculations in R

+
+
+ +
+ + + + +
+ + + + + +
+

Introduction

+

Rounding numbers is a fundamental operation in data analysis and scientific computing. Whether you’re working with financial data, scientific measurements, or large datasets, rounding ensures precision and simplifies results. In R, several functions are available to handle rounding, each tailored to specific needs. This guide will walk you through the most commonly used rounding functions in R—round(), signif(), ceiling(), floor(), and trunc()—with practical examples and real-world applications.

+
+
+

Understanding Rounding in R

+

Rounding is the process of reducing the number of digits in a number while keeping its value close to the original. In R, rounding is essential for:

+
    +
  • Simplifying numerical outputs.
  • +
  • Managing precision in calculations.
  • +
  • Preparing data for visualization or reporting.
  • +
+
+
+

Overview of Rounding Functions in R

+

R provides five primary functions for rounding:

+
    +
  1. round(x, digits = 0): Rounds to the nearest value with a specified number of decimal places.
  2. +
  3. signif(x, digits = 6): Rounds to a specified number of significant digits.
  4. +
  5. ceiling(x): Rounds up to the nearest integer.
  6. +
  7. floor(x): Rounds down to the nearest integer.
  8. +
  9. trunc(x): Truncates the decimal part, effectively rounding towards zero.
  10. +
+

Each function serves a unique purpose, making it crucial to choose the right one for your task.

+
+
+

Using the round() Function

+
+

Syntax

+
round(x, digits = 0)
+
    +
  • x: Numeric vector to be rounded.
  • +
  • digits: Number of decimal places to round to (default is 0).
  • +
+
+
+

Examples

+
    +
  1. Rounding to the nearest integer:

    +
    +
    round(3.14159)  # Output: 3
    +
    +
    [1] 3
    +
    +
  2. +
  3. Rounding to specific decimal places:

    +
    +
    round(3.14159, digits = 2)  # Output: 3.14
    +
    +
    [1] 3.14
    +
    +
  4. +
+
+
+

Use Cases

+
    +
  • Financial calculations (e.g., rounding currency values).
  • +
  • Simplifying outputs for reports.
  • +
+
+
+
+

Using the signif() Function

+
+

Syntax

+
signif(x, digits = 6)
+
    +
  • x: Numeric vector to be rounded.
  • +
  • digits: Number of significant digits.
  • +
+
+
+

Examples

+
    +
  1. Rounding to significant digits:

    +
    +
    signif(12345.6789, digits = 3)  # Output: 12300
    +
    +
    [1] 12300
    +
    +
  2. +
  3. Handling scientific notation:

    +
    +
    signif(0.000123456, digits = 2)  # Output: 0.00012
    +
    +
    [1] 0.00012
    +
    +
  4. +
+
+
+

Use Cases

+
    +
  • Scientific computations requiring precision.
  • +
  • Formatting numbers for publication.
  • +
+
+
+
+

Using the ceiling() Function

+
+

Syntax

+
ceiling(x)
+
    +
  • x: Numeric vector to be rounded up.
  • +
+
+
+

Examples

+
    +
  1. Rounding up positive numbers:

    +
    +
    ceiling(2.3)  # Output: 3
    +
    +
    [1] 3
    +
    +
  2. +
  3. Rounding up negative numbers:

    +
    +
    ceiling(-2.3)  # Output: -2
    +
    +
    [1] -2
    +
    +
  4. +
+
+
+

Use Cases

+
    +
  • Calculating minimum required resources (e.g., rounding up to the nearest whole unit).
  • +
  • Ensuring non-negative results in computations.
  • +
+
+
+
+

Using the floor() Function

+
+

Syntax

+
floor(x)
+
    +
  • x: Numeric vector to be rounded down.
  • +
+
+
+

Examples

+
    +
  1. Rounding down positive numbers:

    +
    +
    floor(2.7)  # Output: 2
    +
    +
    [1] 2
    +
    +
  2. +
  3. Rounding down negative numbers:

    +
    +
    floor(-2.7)  # Output: -3
    +
    +
    [1] -3
    +
    +
  4. +
+
+
+

Use Cases

+
    +
  • Allocating resources conservatively.
  • +
  • Data processing tasks requiring downward rounding.
  • +
+
+
+
+

Using the trunc() Function

+
+

Syntax

+
trunc(x)
+
    +
  • x: Numeric vector to be truncated.
  • +
+
+
+

Examples

+
    +
  1. Truncating positive numbers:

    +
    +
    trunc(3.9)  # Output: 3
    +
    +
    [1] 3
    +
    +
  2. +
  3. Truncating negative numbers:

    +
    +
    trunc(-3.9)  # Output: -3
    +
    +
    [1] -3
    +
    +
  4. +
+
+
+

Use Cases

+
    +
  • Financial calculations where fractional values are ignored.
  • +
  • Simplifying data for integer-based operations.
  • +
+
+
+
+

Comparing Rounding Functions

+ ++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FunctionBehaviorExample InputExample Output
round()Rounds to nearest value2.52
signif()Rounds to significant digits12345.678912300
ceiling()Rounds up to nearest integer2.33
floor()Rounds down to nearest integer2.72
trunc()Truncates decimal places-3.9-3
+
+
+

Practical Examples of Rounding in R

+
+

Example: Financial Data

+
+
prices <- c(19.99, 24.49, 5.75)
+round(prices, digits = 1)  # Output: 20.0, 24.5, 5.8
+
+
[1] 20.0 24.5  5.8
+
+
+
+
+

Example: Scientific Computations

+
+
values <- c(0.000123456, 12345.6789)
+signif(values, digits = 3)  # Output: 0.000123, 12300
+
+
[1] 1.23e-04 1.23e+04
+
+
+
+
+

Example: Data Visualization

+
+
data <- c(2.3, 3.7, 4.1)
+ceiling(data)  # Output: 3, 4, 5
+
+
[1] 3 4 5
+
+
+
+
+
+

Common Pitfalls and How to Avoid Them

+
    +
  1. Misunderstanding “round to even”: +
      +
    • round(0.5) results in 0, not 1.
    • +
  2. +
  3. Floating-point precision issues: +
      +
    • Use all.equal() to compare floating-point numbers.
    • +
  4. +
  5. Inconsistent rounding across platforms: +
      +
    • Be aware of differences between R and other software like MATLAB.
    • +
  6. +
+
+
+

Advanced Rounding Techniques

+
    +
  1. Combining Functions:

    +
    +
    x <- c(3.14159, 2.71828)
    +round(floor(x), digits = 1)  # Output: 3.0, 2.0
    +
    +
    [1] 3 2
    +
    +
  2. +
  3. Rounding in Data Frames:

    +
    +
    df <- data.frame(a = c(1.234, 5.678), b = c(9.876, 3.210))
    +df[] <- lapply(df, round, digits = 2)
    +
  4. +
+
+
+

Your Turn!

+

Problem: Round the following vector using all five functions:

+
x <- c(3.14159, 2.71828, 1.61803, -1.41421, -2.23607)
+
+ +Click here for Solution! + +

Solution:

+
+
x <- c(3.14159, 2.71828, 1.61803, -1.41421, -2.23607)
+
+data.frame(
+  Original = x,
+  Rounded = round(x, digits = 2),
+  Significant = signif(x, digits = 3),
+  Ceiled = ceiling(x),
+  Floored = floor(x),
+  Truncated = trunc(x)
+)
+
+
  Original Rounded Significant Ceiled Floored Truncated
+1  3.14159    3.14        3.14      4       3         3
+2  2.71828    2.72        2.72      3       2         2
+3  1.61803    1.62        1.62      2       1         1
+4 -1.41421   -1.41       -1.41     -1      -2        -1
+5 -2.23607   -2.24       -2.24     -2      -3        -2
+
+
+
+
+
+

Quick Takeaways

+
    +
  • Rounding functions in R include round(), signif(), ceiling(), floor(), and trunc().
  • +
  • Each function has unique use cases, from financial calculations to scientific precision.
  • +
  • Understanding their behavior ensures accurate and efficient data analysis.
  • +
+
+
+

Conclusion

+

Rounding numbers in R is a versatile and essential skill for programmers. By mastering these functions, you can handle a wide range of tasks with precision and confidence. Start applying these techniques in your projects today!

+
+
+

FAQs

+
    +
  1. What is the difference between round() and signif()?
    +round() focuses on decimal places, while signif() targets significant digits.

  2. +
  3. How does ceiling() handle negative numbers?
    +It rounds up towards zero.

  4. +
  5. Can I use rounding functions on data frames?
    +Yes, use apply() or dplyr for efficient operations.

  6. +
  7. What happens when rounding 0.5 in R?
    +R uses “round to even,” so 0.5 rounds to 0.

  8. +
  9. **Are there alternatives to these functions?

  10. +
+
+
+

References

+
    +
  1. R Documentation: round()
    +Official documentation for the round() function in R, detailing its syntax, parameters, and behavior. Available at: https://www.rdocumentation.org/packages/base/versions/latest/topics/round

  2. +
  3. R Documentation: signif()
    +Official documentation for the signif() function in R, explaining its usage for rounding to significant digits. Available at: https://www.rdocumentation.org/packages/base/versions/latest/topics/signif

  4. +
  5. R Documentation: ceiling()
    +Official documentation for the ceiling() function in R, describing its functionality for rounding up to the nearest integer. Available at: https://www.rdocumentation.org/packages/base/versions/latest/topics/ceiling

  6. +
  7. R Documentation: floor()
    +Official documentation for the floor() function in R, which rounds down to the nearest integer. Available at: https://www.rdocumentation.org/packages/base/versions/latest/topics/floor

  8. +
  9. R Documentation: trunc()
    +Official documentation for the trunc() function in R, explaining its use for truncating decimal places. Available at: https://www.rdocumentation.org/packages/base/versions/latest/topics/trunc

  10. +
+
+

Happy Coding! 🚀

+
+
+

+
Rounding in R
+
+
+
+

You can connect with me at any one of the below:

+

Telegram Channel here: https://t.me/steveondata

+

LinkedIn Network here: https://www.linkedin.com/in/spsanderson/

+

Mastadon Social here: https://mstdn.social/@stevensanderson

+

RStats Network here: https://rstats.me/@spsanderson

+

GitHub Network here: https://github.com/spsanderson

+

Bluesky Network here: https://bsky.app/profile/spsanderson.com

+

My Book: Extending Excel with Python and R here: https://packt.link/oTyZJ

+
+ + + +
+ +
+ +
+ + + + + \ No newline at end of file diff --git a/docs/posts/2024-12-31/todays_post.png b/docs/posts/2024-12-31/todays_post.png new file mode 100644 index 00000000..207ccf37 Binary files /dev/null and b/docs/posts/2024-12-31/todays_post.png differ diff --git a/docs/search.json b/docs/search.json index e6781bef..9291e287 100644 --- a/docs/search.json +++ b/docs/search.json @@ -14775,5 +14775,138 @@ "title": "Comprehensive Guide to Arcsine Transformation in R with Examples", "section": "5. Is the arcsine transformation suitable for all types of data?", "text": "5. Is the arcsine transformation suitable for all types of data?\nNo, it is specifically designed for proportional data. Other transformations may be more appropriate for different data types." + }, + { + "objectID": "posts/2024-12-31/index.html", + "href": "posts/2024-12-31/index.html", + "title": "Rounding Numbers in R with Examples: A Comprehensive Guide", + "section": "", + "text": "Rounding numbers is a fundamental operation in data analysis and scientific computing. Whether you’re working with financial data, scientific measurements, or large datasets, rounding ensures precision and simplifies results. In R, several functions are available to handle rounding, each tailored to specific needs. This guide will walk you through the most commonly used rounding functions in R—round(), signif(), ceiling(), floor(), and trunc()—with practical examples and real-world applications." + }, + { + "objectID": "posts/2024-12-31/index.html#syntax", + "href": "posts/2024-12-31/index.html#syntax", + "title": "Rounding Numbers in R with Examples: A Comprehensive Guide", + "section": "Syntax", + "text": "Syntax\nround(x, digits = 0)\n\nx: Numeric vector to be rounded.\ndigits: Number of decimal places to round to (default is 0)." + }, + { + "objectID": "posts/2024-12-31/index.html#examples", + "href": "posts/2024-12-31/index.html#examples", + "title": "Rounding Numbers in R with Examples: A Comprehensive Guide", + "section": "Examples", + "text": "Examples\n\nRounding to the nearest integer:\n\nround(3.14159) # Output: 3\n\n[1] 3\n\n\nRounding to specific decimal places:\n\nround(3.14159, digits = 2) # Output: 3.14\n\n[1] 3.14" + }, + { + "objectID": "posts/2024-12-31/index.html#use-cases", + "href": "posts/2024-12-31/index.html#use-cases", + "title": "Rounding Numbers in R with Examples: A Comprehensive Guide", + "section": "Use Cases", + "text": "Use Cases\n\nFinancial calculations (e.g., rounding currency values).\nSimplifying outputs for reports." + }, + { + "objectID": "posts/2024-12-31/index.html#syntax-1", + "href": "posts/2024-12-31/index.html#syntax-1", + "title": "Rounding Numbers in R with Examples: A Comprehensive Guide", + "section": "Syntax", + "text": "Syntax\nsignif(x, digits = 6)\n\nx: Numeric vector to be rounded.\ndigits: Number of significant digits." + }, + { + "objectID": "posts/2024-12-31/index.html#examples-1", + "href": "posts/2024-12-31/index.html#examples-1", + "title": "Rounding Numbers in R with Examples: A Comprehensive Guide", + "section": "Examples", + "text": "Examples\n\nRounding to significant digits:\n\nsignif(12345.6789, digits = 3) # Output: 12300\n\n[1] 12300\n\n\nHandling scientific notation:\n\nsignif(0.000123456, digits = 2) # Output: 0.00012\n\n[1] 0.00012" + }, + { + "objectID": "posts/2024-12-31/index.html#use-cases-1", + "href": "posts/2024-12-31/index.html#use-cases-1", + "title": "Rounding Numbers in R with Examples: A Comprehensive Guide", + "section": "Use Cases", + "text": "Use Cases\n\nScientific computations requiring precision.\nFormatting numbers for publication." + }, + { + "objectID": "posts/2024-12-31/index.html#syntax-2", + "href": "posts/2024-12-31/index.html#syntax-2", + "title": "Rounding Numbers in R with Examples: A Comprehensive Guide", + "section": "Syntax", + "text": "Syntax\nceiling(x)\n\nx: Numeric vector to be rounded up." + }, + { + "objectID": "posts/2024-12-31/index.html#examples-2", + "href": "posts/2024-12-31/index.html#examples-2", + "title": "Rounding Numbers in R with Examples: A Comprehensive Guide", + "section": "Examples", + "text": "Examples\n\nRounding up positive numbers:\n\nceiling(2.3) # Output: 3\n\n[1] 3\n\n\nRounding up negative numbers:\n\nceiling(-2.3) # Output: -2\n\n[1] -2" + }, + { + "objectID": "posts/2024-12-31/index.html#use-cases-2", + "href": "posts/2024-12-31/index.html#use-cases-2", + "title": "Rounding Numbers in R with Examples: A Comprehensive Guide", + "section": "Use Cases", + "text": "Use Cases\n\nCalculating minimum required resources (e.g., rounding up to the nearest whole unit).\nEnsuring non-negative results in computations." + }, + { + "objectID": "posts/2024-12-31/index.html#syntax-3", + "href": "posts/2024-12-31/index.html#syntax-3", + "title": "Rounding Numbers in R with Examples: A Comprehensive Guide", + "section": "Syntax", + "text": "Syntax\nfloor(x)\n\nx: Numeric vector to be rounded down." + }, + { + "objectID": "posts/2024-12-31/index.html#examples-3", + "href": "posts/2024-12-31/index.html#examples-3", + "title": "Rounding Numbers in R with Examples: A Comprehensive Guide", + "section": "Examples", + "text": "Examples\n\nRounding down positive numbers:\n\nfloor(2.7) # Output: 2\n\n[1] 2\n\n\nRounding down negative numbers:\n\nfloor(-2.7) # Output: -3\n\n[1] -3" + }, + { + "objectID": "posts/2024-12-31/index.html#use-cases-3", + "href": "posts/2024-12-31/index.html#use-cases-3", + "title": "Rounding Numbers in R with Examples: A Comprehensive Guide", + "section": "Use Cases", + "text": "Use Cases\n\nAllocating resources conservatively.\nData processing tasks requiring downward rounding." + }, + { + "objectID": "posts/2024-12-31/index.html#syntax-4", + "href": "posts/2024-12-31/index.html#syntax-4", + "title": "Rounding Numbers in R with Examples: A Comprehensive Guide", + "section": "Syntax", + "text": "Syntax\ntrunc(x)\n\nx: Numeric vector to be truncated." + }, + { + "objectID": "posts/2024-12-31/index.html#examples-4", + "href": "posts/2024-12-31/index.html#examples-4", + "title": "Rounding Numbers in R with Examples: A Comprehensive Guide", + "section": "Examples", + "text": "Examples\n\nTruncating positive numbers:\n\ntrunc(3.9) # Output: 3\n\n[1] 3\n\n\nTruncating negative numbers:\n\ntrunc(-3.9) # Output: -3\n\n[1] -3" + }, + { + "objectID": "posts/2024-12-31/index.html#use-cases-4", + "href": "posts/2024-12-31/index.html#use-cases-4", + "title": "Rounding Numbers in R with Examples: A Comprehensive Guide", + "section": "Use Cases", + "text": "Use Cases\n\nFinancial calculations where fractional values are ignored.\nSimplifying data for integer-based operations." + }, + { + "objectID": "posts/2024-12-31/index.html#example-financial-data", + "href": "posts/2024-12-31/index.html#example-financial-data", + "title": "Rounding Numbers in R with Examples: A Comprehensive Guide", + "section": "Example: Financial Data", + "text": "Example: Financial Data\n\nprices <- c(19.99, 24.49, 5.75)\nround(prices, digits = 1) # Output: 20.0, 24.5, 5.8\n\n[1] 20.0 24.5 5.8" + }, + { + "objectID": "posts/2024-12-31/index.html#example-scientific-computations", + "href": "posts/2024-12-31/index.html#example-scientific-computations", + "title": "Rounding Numbers in R with Examples: A Comprehensive Guide", + "section": "Example: Scientific Computations", + "text": "Example: Scientific Computations\n\nvalues <- c(0.000123456, 12345.6789)\nsignif(values, digits = 3) # Output: 0.000123, 12300\n\n[1] 1.23e-04 1.23e+04" + }, + { + "objectID": "posts/2024-12-31/index.html#example-data-visualization", + "href": "posts/2024-12-31/index.html#example-data-visualization", + "title": "Rounding Numbers in R with Examples: A Comprehensive Guide", + "section": "Example: Data Visualization", + "text": "Example: Data Visualization\n\ndata <- c(2.3, 3.7, 4.1)\nceiling(data) # Output: 3, 4, 5\n\n[1] 3 4 5" } ] \ No newline at end of file diff --git a/docs/sitemap.xml b/docs/sitemap.xml index 0d742375..2cd81e3b 100644 --- a/docs/sitemap.xml +++ b/docs/sitemap.xml @@ -910,7 +910,7 @@ https://www.spsanderson.com/steveondata/index.html - 2023-03-28T12:23:03.885Z + 2022-11-16T15:17:41.340Z https://www.spsanderson.com/steveondata/about.html @@ -2018,6 +2018,10 @@ https://www.spsanderson.com/steveondata/posts/2024-12-30/index.html - 2024-12-30T12:21:43.390Z + 2024-12-31T02:08:29.716Z + + + https://www.spsanderson.com/steveondata/posts/2024-12-31/index.html + 2024-12-31T02:42:47.102Z diff --git a/posts/2024-12-31/index.qmd b/posts/2024-12-31/index.qmd new file mode 100644 index 00000000..b17da02a --- /dev/null +++ b/posts/2024-12-31/index.qmd @@ -0,0 +1,339 @@ +--- +title: "Rounding Numbers in R with Examples: A Comprehensive Guide" +author: "Steven P. Sanderson II, MPH" +date: "2024-12-31" +categories: [code, rtip] +toc: TRUE +description: "Master rounding numbers in R with this detailed guide. Learn how to use `round()`, `signif()`, `ceiling()`, `floor()`, and `trunc()` functions with practical examples." +keywords: [Programming, Rounding Numbers in R, R Programming Functions, Rounding Functions, Data Analysis in R, Numerical Precision in R, round() function in R, signif() function in R, ceiling() function in R, floor() function in R, trunc() function in R, How to round numbers in R programming, Examples of rounding functions in R, Using ceiling and floor functions in R, Understanding significant digits in R, Best practices for rounding in data analysis with R, R rounding functions, R programming rounding examples, How to round numbers in R, R round() function tutorial, R signif() function examples, R ceiling() vs floor(), R truncation examples, Rounding decimals in R, Rounding integers in R, Rounding techniques in R programming, Rounding for data analysis in R, Rounding edge cases in R, Rounding performance in R, Rounding in R data frames, Rounding for, financial calculations in R] +draft: TRUE +--- + +# Introduction + +Rounding numbers is a fundamental operation in data analysis and scientific computing. Whether you're working with financial data, scientific measurements, or large datasets, rounding ensures precision and simplifies results. In R, several functions are available to handle rounding, each tailored to specific needs. This guide will walk you through the most commonly used rounding functions in R—`round()`, `signif()`, `ceiling()`, `floor()`, and `trunc()`—with practical examples and real-world applications. + +# Understanding Rounding in R + +Rounding is the process of reducing the number of digits in a number while keeping its value close to the original. In R, rounding is essential for: + +- Simplifying numerical outputs. +- Managing precision in calculations. +- Preparing data for visualization or reporting. + +# Overview of Rounding Functions in R + +R provides five primary functions for rounding: + +1. **`round(x, digits = 0)`**: Rounds to the nearest value with a specified number of decimal places. +2. **`signif(x, digits = 6)`**: Rounds to a specified number of significant digits. +3. **`ceiling(x)`**: Rounds up to the nearest integer. +4. **`floor(x)`**: Rounds down to the nearest integer. +5. **`trunc(x)`**: Truncates the decimal part, effectively rounding towards zero. + +Each function serves a unique purpose, making it crucial to choose the right one for your task. + +# Using the `round()` Function + +## **Syntax** + +```R +round(x, digits = 0) +``` +- `x`: Numeric vector to be rounded. +- `digits`: Number of decimal places to round to (default is 0). + +## **Examples** + +1. **Rounding to the nearest integer**: + + ```{r} + round(3.14159) # Output: 3 + ``` +2. **Rounding to specific decimal places**: + + ```{r} + round(3.14159, digits = 2) # Output: 3.14 + ``` + +## **Use Cases** + +- Financial calculations (e.g., rounding currency values). +- Simplifying outputs for reports. + +# Using the `signif()` Function + +## **Syntax** + +```R +signif(x, digits = 6) +``` +- `x`: Numeric vector to be rounded. +- `digits`: Number of significant digits. + +## **Examples** + +1. **Rounding to significant digits**: + + ```{r} + signif(12345.6789, digits = 3) # Output: 12300 + ``` +2. **Handling scientific notation**: + + ```{r} + signif(0.000123456, digits = 2) # Output: 0.00012 + ``` + +## **Use Cases** + +- Scientific computations requiring precision. +- Formatting numbers for publication. + +# Using the `ceiling()` Function + +## **Syntax** + +```R +ceiling(x) +``` +- `x`: Numeric vector to be rounded up. + +## **Examples** + +1. **Rounding up positive numbers**: + + ```{r} + ceiling(2.3) # Output: 3 + ``` +2. **Rounding up negative numbers**: + + ```{r} + ceiling(-2.3) # Output: -2 + ``` + +## **Use Cases** + +- Calculating minimum required resources (e.g., rounding up to the nearest whole unit). +- Ensuring non-negative results in computations. + +# Using the `floor()` Function + +## **Syntax** + +```R +floor(x) +``` +- `x`: Numeric vector to be rounded down. + +## **Examples** + +1. **Rounding down positive numbers**: + + ```{r} + floor(2.7) # Output: 2 + ``` +2. **Rounding down negative numbers**: + + ```{r} + floor(-2.7) # Output: -3 + ``` + +## **Use Cases** + +- Allocating resources conservatively. +- Data processing tasks requiring downward rounding. + +# Using the `trunc()` Function + +## **Syntax** + +```R +trunc(x) +``` +- `x`: Numeric vector to be truncated. + +## **Examples** + +1. **Truncating positive numbers**: + + ```{r} + trunc(3.9) # Output: 3 + ``` +2. **Truncating negative numbers**: + + ```{r} + trunc(-3.9) # Output: -3 + ``` + +## **Use Cases** + +- Financial calculations where fractional values are ignored. +- Simplifying data for integer-based operations. + +# Comparing Rounding Functions + +| Function | Behavior | Example Input | Example Output | +|------------|-----------------------------------|---------------|----------------| +| `round()` | Rounds to nearest value | 2.5 | 2 | +| `signif()` | Rounds to significant digits | 12345.6789 | 12300 | +| `ceiling()`| Rounds up to nearest integer | 2.3 | 3 | +| `floor()` | Rounds down to nearest integer | 2.7 | 2 | +| `trunc()` | Truncates decimal places | -3.9 | -3 | + +# Practical Examples of Rounding in R + +## **Example: Financial Data** + +```{r} +prices <- c(19.99, 24.49, 5.75) +round(prices, digits = 1) # Output: 20.0, 24.5, 5.8 +``` + +## **Example: Scientific Computations** + +```{r} +values <- c(0.000123456, 12345.6789) +signif(values, digits = 3) # Output: 0.000123, 12300 +``` + +## **Example: Data Visualization** + +```{r} +data <- c(2.3, 3.7, 4.1) +ceiling(data) # Output: 3, 4, 5 +``` + +# Common Pitfalls and How to Avoid Them + +1. **Misunderstanding "round to even"**: + - `round(0.5)` results in 0, not 1. +2. **Floating-point precision issues**: + - Use `all.equal()` to compare floating-point numbers. +3. **Inconsistent rounding across platforms**: + - Be aware of differences between R and other software like MATLAB. + +# Advanced Rounding Techniques + +1. **Combining Functions**: + + ```{r} + x <- c(3.14159, 2.71828) + round(floor(x), digits = 1) # Output: 3.0, 2.0 + ``` +2. **Rounding in Data Frames**: + + ```{r} + df <- data.frame(a = c(1.234, 5.678), b = c(9.876, 3.210)) + df[] <- lapply(df, round, digits = 2) + ``` +# Your Turn! + +**Problem**: Round the following vector using all five functions: + +```r +x <- c(3.14159, 2.71828, 1.61803, -1.41421, -2.23607) +``` +
Click here for Solution! +**Solution**: + +```{r} +x <- c(3.14159, 2.71828, 1.61803, -1.41421, -2.23607) + +data.frame( + Original = x, + Rounded = round(x, digits = 2), + Significant = signif(x, digits = 3), + Ceiled = ceiling(x), + Floored = floor(x), + Truncated = trunc(x) +) +``` +
+ +# Quick Takeaways + +- Rounding functions in R include `round()`, `signif()`, `ceiling()`, `floor()`, and `trunc()`. +- Each function has unique use cases, from financial calculations to scientific precision. +- Understanding their behavior ensures accurate and efficient data analysis. + +# Conclusion + +Rounding numbers in R is a versatile and essential skill for programmers. By mastering these functions, you can handle a wide range of tasks with precision and confidence. Start applying these techniques in your projects today! + +# FAQs + +1. **What is the difference between `round()` and `signif()`?** + `round()` focuses on decimal places, while `signif()` targets significant digits. + +2. **How does `ceiling()` handle negative numbers?** + It rounds up towards zero. + +3. **Can I use rounding functions on data frames?** + Yes, use `apply()` or `dplyr` for efficient operations. + +4. **What happens when rounding 0.5 in R?** + R uses "round to even," so 0.5 rounds to 0. + +5. **Are there alternatives to these functions? + +# References + +1. **R Documentation: `round()`** + Official documentation for the `round()` function in R, detailing its syntax, parameters, and behavior. Available at: [https://www.rdocumentation.org/packages/base/versions/latest/topics/round](https://www.rdocumentation.org/packages/base/versions/latest/topics/round) + +2. **R Documentation: `signif()`** + Official documentation for the `signif()` function in R, explaining its usage for rounding to significant digits. Available at: [https://www.rdocumentation.org/packages/base/versions/latest/topics/signif](https://www.rdocumentation.org/packages/base/versions/latest/topics/signif) + +3. **R Documentation: `ceiling()`** + Official documentation for the `ceiling()` function in R, describing its functionality for rounding up to the nearest integer. Available at: [https://www.rdocumentation.org/packages/base/versions/latest/topics/ceiling](https://www.rdocumentation.org/packages/base/versions/latest/topics/ceiling) + +4. **R Documentation: `floor()`** + Official documentation for the `floor()` function in R, which rounds down to the nearest integer. Available at: [https://www.rdocumentation.org/packages/base/versions/latest/topics/floor](https://www.rdocumentation.org/packages/base/versions/latest/topics/floor) + +5. **R Documentation: `trunc()`** + Official documentation for the `trunc()` function in R, explaining its use for truncating decimal places. Available at: [https://www.rdocumentation.org/packages/base/versions/latest/topics/trunc](https://www.rdocumentation.org/packages/base/versions/latest/topics/trunc) + +------------------------------------------------------------------------ + +Happy Coding! 🚀 + +![Rounding in R](todays_post.png) + +------------------------------------------------------------------------ + +*You can connect with me at any one of the below*: + +*Telegram Channel here*: + +*LinkedIn Network here*: + +*Mastadon Social here*: [https://mstdn.social/\@stevensanderson](https://mstdn.social/@stevensanderson) + +*RStats Network here*: [https://rstats.me/\@spsanderson](https://rstats.me/@spsanderson) + +*GitHub Network here*: + +*Bluesky Network here*: + +*My Book: Extending Excel with Python and R* here: + +------------------------------------------------------------------------ + +```{=html} + +``` diff --git a/posts/2024-12-31/todays_post.png b/posts/2024-12-31/todays_post.png new file mode 100644 index 00000000..207ccf37 Binary files /dev/null and b/posts/2024-12-31/todays_post.png differ