R Numeric Data Type
The numeric data type stores numbers — both whole numbers and decimals. It is the most commonly used data type in R because data analysis revolves around numbers. Whenever you create a variable with a number value, R stores it as numeric by default.
What Is Numeric?
Data Type: Numeric ────────────────────────────────────────────── Examples: 42, 3.14, -7, 0.001, 1000000 Stores: Integers and decimal numbers Default: All plain numbers in R are numeric Alias: Also called "double" (double precision)
Creating Numeric Variables
weight <- 72.5 distance <- 1500 temperature <- -3.2 pi_approx <- 3.14159 class(weight) # "numeric" class(distance) # "numeric"
R treats every number you type — whether it is 5, 5.0, or 5.5 — as numeric unless you explicitly tell it otherwise.
Arithmetic with Numeric Values
a <- 48 b <- 7 a + b # 55 a - b # 41 a * b # 336 a / b # 6.857143 a %% b # 6 (remainder when 48 divided by 7) a %/% b # 6 (integer division — whole part only) a ^ 2 # 2304
Special Numeric Values
R has three special numeric values you will encounter in real data analysis.
Value Meaning Example Cause ────────────────────────────────────────────────────────── Inf Positive infinity 1 / 0 -Inf Negative infinity -1 / 0 NaN Not a Number (undefined) 0 / 0
1 / 0 # Inf -1 / 0 # -Inf 0 / 0 # NaN is.infinite(1/0) # TRUE is.nan(0/0) # TRUE
Precision of Numeric Values
R stores numeric values with up to about 15 significant digits. For example:
x <- 0.1 + 0.2 print(x) # 0.3 (appears correct) x == 0.3 # FALSE (floating-point issue!) all.equal(x, 0.3) # TRUE (correct way to compare)
Computers cannot always represent decimals exactly in binary. Use all.equal() instead of == when comparing decimal numbers.
Checking and Converting Numeric
is.numeric(42.5) # TRUE
is.numeric("hello") # FALSE
as.numeric("3.14") # 3.14 — converts text to number
as.numeric(TRUE) # 1 — TRUE becomes 1
as.numeric(FALSE) # 0 — FALSE becomes 0
Useful Numeric Functions
Function What It Does Example Output ────────────────────────────────────────────────────── abs(-15) Absolute value 15 sqrt(81) Square root 9 ceiling(4.2) Round up 5 floor(4.9) Round down 4 round(3.567, 2) Round to 2 decimals 3.57 sum(1,2,3,4) Add all values 10 max(5,2,9,1) Largest value 9 min(5,2,9,1) Smallest value 1
A Practical Example: BMI Calculator
# Body Mass Index calculation
weight_kg <- 70
height_m <- 1.75
bmi <- weight_kg / (height_m ^ 2)
bmi <- round(bmi, 1)
cat("BMI:", bmi, "\n")
Output:
BMI: 22.9
Numeric Range in R
Smallest positive number: .Machine$double.eps (about 2.2e-16) Largest number: .Machine$double.xmax (about 1.8e+308)
For nearly all real-world data analysis tasks, these limits are far beyond what you will ever need. R handles financial figures, scientific measurements, and large statistical datasets comfortably within this range.
Numeric is the default and most fundamental data type in R. Every dataset you work with will contain numeric columns, and mastering how to create, manipulate, and check numeric values gives you the tools to work with real data effectively.
