![]() |
R is a popular programming language and environment for statistical computing and graphics, providing a variety of built-in constants that are useful for different types of data analysis and scientific computations. Understanding these constants is essential for efficient coding and accurate results. What are Built-in Constants?Built-in constants in R are predefined values that are universally recognized within the R environment. These constants simplify coding by providing commonly used values that are often needed in various calculations and data processing tasks. They cover a range of areas including mathematical constants, machine limits, and logical values. Mathematical ConstantsR Programming Language includes several important mathematical constants which are frequently used in statistical computations and mathematical modeling. 1. Pi (π)The constant pi in R represents the ratio of the circumference of a circle to its diameter, approximately 3.14159. print(pi) 2. InfinityR uses Inf to represent positive infinity and -Inf for negative infinity. These are useful in mathematical computations that result in values beyond the largest representable number. e <- exp(1) 3. Not a Number (NaN)The constant NaN stands for “Not a Number” and is used to represent undefined or unrepresentable values, particularly in floating-point calculations. print(NaN) 4. InfThis constant represents positive infinity. It is useful for representing values that are infinitely large. print(Inf) 5. -InfThis constant represents negative infinity. print(-Inf) 6. TRUE and FALSE These constants represent logical true and false values. print(TRUE) Machine Limits and Special Numerical ValuesR also provides several built-in constants that represent machine limits and special numerical values: 1: .Machine: This is a list of machine-specific numerical constants, which include limits and characteristics of the computer’s floating-point arithmetic. Accessing this list provides useful information about numerical precision and range. print(.Machine) Some important components of .Machine include:
2: letters and LETTERS: These constants represent the lowercase and uppercase English alphabets, respectively. print(letters) 3: month.abb and month.name: These constants provide the abbreviated and full names of the months, respectively. print(month.abb) Here are some examples that demonstrate the usage of built-in constants in R Programming Language. Example 1: Calculating the Circumference of a CircleUsing the constant
Output: [1] 31.41593 Example 2: Handling Missing ValuesUsing the constant
Output: [1] 3 Example 3: Checking Numerical PrecisionUsing the constant
Output: [1] FALSE ConclusionBuilt-in constants in R are invaluable tools that simplify coding and ensure accuracy in computations. From mathematical constants like pi and Inf to logical values like TRUE and FALSE, these predefined values are integral to the R programming environment. By understanding and utilizing these constants, R users can enhance their data analysis and statistical modeling capabilities. |
Reffered: https://www.geeksforgeeks.org
R Language |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 19 |