đ Introduction
A constant is a fixed value that does not change during the execution of a program. Unlike variables, whose values can be modified, constants represent values that remain the same throughout the program. Although R does not provide a built-in mechanism for declaring true constants, programmers commonly use variables with a naming convention to indicate that their values should not be changed.
Information
đ¯ Why Use Constants?
- Improve code readability.
- Avoid using "magic numbers" throughout the program.
- Make programs easier to maintain.
- Reduce the chance of accidental changes.
- Provide meaningful names for fixed values.
đ Declaring Constants
Since R has no built-in constant declaration, fixed values are usually stored in variables named using uppercase letters to indicate that they should not be modified.
Declaring Constants
PI_VALUE <- 3.14159
MAX_MARKS <- 100
COMPANY_NAME <- "Open Learning"
print(PI_VALUE)
print(MAX_MARKS)
print(COMPANY_NAME)Output
Console Output
[1] 3.14159
[1] 100
[1] "Open Learning"đ Types of Constants
Numeric constants represent integer or decimal values.
Numeric Constants
RATE <- 12.5
YEAR <- 2026
print(RATE)
print(YEAR)Character constants are strings enclosed in quotation marks.
Character Constants
GREETING <- "Welcome to R"
print(GREETING)Logical constants represent Boolean values.
Logical Constants
IS_ACTIVE <- TRUE
IS_ADMIN <- FALSE
print(IS_ACTIVE)
print(IS_ADMIN)R also provides several predefined special constants.
Special Constants
print(NULL)
print(NA)
print(Inf)
print(NaN)đĸ Built-in Constants in R
R includes several predefined constants that are frequently used in calculations and data analysis.
| Constant | Description | Example |
|---|---|---|
| pi | Value of Ī (approximately 3.141593) | print(pi) |
| LETTERS | Uppercase English alphabet | print(LETTERS) |
| letters | Lowercase English alphabet | print(letters) |
| month.name | Full month names | print(month.name) |
| month.abb | Abbreviated month names | print(month.abb) |
đ§Ž Using Constants in Calculations
Constants make formulas easier to understand and maintain by replacing fixed numeric values with descriptive names.
Area of a Circle
PI_VALUE <- 3.14159
radius <- 7
area <- PI_VALUE * radius^2
print(area)Output
Console Output
[1] 153.9379đ Constant Naming Convention
đ Real-World Example
Employee Bonus Calculation
BONUS_RATE <- 0.10
salary <- 50000
bonus <- salary * BONUS_RATE
print(bonus)Output
Console Output
[1] 5000â ī¸ Can Constants Be Changed?
Because constants in R are implemented using ordinary variables, their values can technically be reassigned. However, changing a value that is intended to remain constant is considered poor programming practice.
Reassigning a Constant
MAX_USERS <- 100
print(MAX_USERS)
MAX_USERS <- 200
print(MAX_USERS)Warning
đ Variables vs Constants
| Feature | Variable | Constant |
|---|---|---|
| Value Changes | Yes | Ideally No |
| Declaration | Regular assignment | Regular assignment with naming convention |
| Naming Style | studentName | MAX_SIZE |
| Purpose | Store changing data | Store fixed values |
đĄ Best Practices
- Use uppercase names for constants.
- Choose descriptive and meaningful names.
- Avoid changing constant values after assignment.
- Use constants instead of repeating fixed values throughout the program.
- Prefer built-in constants like pi whenever available.
Best Practice
đ Summary
Constants represent values that are intended to remain unchanged throughout a program. Although R does not support true constants through a dedicated keyword, developers commonly use uppercase variable names to indicate fixed values. By using constants for configuration values, mathematical formulas, and predefined settings, you can write cleaner, more readable, and more maintainable R programs.