R Numbers
R handles numeric values in different ways, and understanding how numbers work is essential for data analysis. R numbers can be divided into the following categories:
Key Topics
1. Numeric Values
In R, numeric values are numbers with or without decimals. By default, numbers in R are treated as numeric (or double precision).
# Example of numeric values
num1 <- 10.75
num2 <- 3.5
sum <- num1 + num2
print(sum)
Output:
Code Explanation: num1
and num2
are numeric values. The sum is calculated and printed, showing 14.25.
2. Integer Values
Integer values in R are whole numbers. You need to use the L
suffix to specify an integer explicitly.
# Example of integer values
int1 <- 5L
int2 <- 15L
product <- int1 * int2
print(product)
Output:
Code Explanation: int1
and int2
are integers, specified with the L
suffix. Their product is calculated and printed as 75.
3. Special Values (Inf, -Inf, NaN)
R has special numeric values: Inf
(Infinity), -Inf
(Negative Infinity), and NaN
(Not a Number).
# Examples of special values
value1 <- 1 / 0 # Inf
value2 <- -1 / 0 # -Inf
value3 <- 0 / 0 # NaN
print(value1)
print(value2)
print(value3)
Output:
[1] -Inf
[1] NaN
Code Explanation: value1
is Inf
(Infinity) because dividing a number by zero results in infinity. value2
is -Inf
(Negative Infinity). value3
is NaN
(Not a Number) because dividing zero by zero is undefined.
Key Takeaways
- R handles both decimal (numeric) and whole numbers (integer).
- Use the
L
suffix to specify an integer explicitly. - R provides special values:
Inf
,-Inf
, andNaN
to handle mathematical edge cases.