Numbers are the bread and butter of R. But R has a few special number-like values that might trip you up if you haven't seen them before. Let's walk through how R handles arithmetic and those quirky edge cases.
Arithmetic with Numbers
Standard math works just like you'd expect. R supports addition, subtraction, multiplication, division, exponents, modulo (remainder), and integer division. You can combine them with parentheses to control the order of operations.
# All the basic operations
10 + 3
10 - 3
10 * 3
10 / 3
10 ^ 3
10 %% 3
10 %/% 3
(1 + 2) * 3
Try it Yourself →
Inf and -Inf
If you divide a number by zero, R doesn't crash — it returns Inf (infinity). Same idea for -Inf. It's R's way of saying "this number is too big to exist" or "this direction goes forever." You can even do math with infinity, which feels weird but is occasionally useful.
# Infinity
1 / 0
-1 / 0
Inf + 1
Try it Yourself →
NaN — Not a Number
Some operations don't produce a meaningful number. For example, 0 / 0 or Inf - Inf. When that happens, R returns NaN (Not a Number). It's a signal that the math didn't make sense. Any calculation involving NaN also returns NaN, so it spreads like a virus. Watch for it.
# Not a Number
0 / 0
Inf - Inf
NaN + 5
Try it Yourself →
NA — Missing Values
NA stands for Not Available. It's R's way of representing missing data. Real datasets almost always have gaps — a survey question left blank, a sensor that failed, a record that wasn't collected. R doesn't ignore NAs; it flags them. Many functions have an na.rm parameter that tells them to remove NAs before calculating.
# Missing values
height <- c(150, 160, NA, 170)
mean(height)
mean(height, na.rm = TRUE)
Try it Yourself →
Integer vs Double
R stores numbers as doubles (decimal) by default. If you want an integer, add L after the number. The difference matters for memory and precision. Doubles can store huge ranges of numbers with decimals, while integers are exact whole numbers. Usually you won't notice the difference, but when you start working with big data, integers save space.
# Integer vs double
x <- 5 # numeric (double)
y <- 5L # integer
class(x)
class(y)
Try it Yourself →
Numbers are straightforward in R, but Inf, NaN, and NA catch everyone at least once. Now let's move on to something more fun — working with text.