R Integer Data Type
An integer is a whole number with no decimal part. Examples are 1, 42, -7, and 1000. In R, plain numbers are stored as numeric (decimal) by default, even if you type 5. To store a number specifically as an integer, you add the letter L right after it.
Numeric vs Integer — What's the Difference?
Numeric (default) Integer (explicit)
─────────────────────────────────────────────────────────
Value 5.0 (with decimals) 5L (whole number only)
Memory 8 bytes 4 bytes
Type "numeric" "integer"
Example weight <- 72.5 count <- 100L
Integer uses less memory than numeric because it does not need to store decimal information. For large datasets with millions of whole-number values, this difference becomes significant.
Creating Integer Variables
student_count <- 35L floor_number <- 7L temperature_c <- -4L class(student_count) # "integer" class(35) # "numeric" (no L = numeric) class(35L) # "integer" (L = integer)
Checking for Integer
is.integer(10L) # TRUE is.integer(10) # FALSE (it's numeric) is.numeric(10L) # TRUE (integer IS a subset of numeric)
This is an important point: every integer is also considered numeric in R, but not every numeric is an integer.
All Numbers in R ────────────────────────────────────── ┌──────────────────────────────────┐ │ Numeric │ │ ┌──────────────────────────┐ │ │ │ Integer │ │ │ │ 35L, -7L, 0L, 1000L │ │ │ └──────────────────────────┘ │ │ 3.14, -2.5, 0.001, 1e6 │ └──────────────────────────────────┘
Converting Between Integer and Numeric
x <- 42.9 # numeric as.integer(x) # 42 — truncates decimal (does NOT round) y <- 15L # integer as.numeric(y) # 15 — converts to numeric
Notice that as.integer() truncates (cuts off) the decimal part. It does not round. So as.integer(9.9) gives 9, not 10.
Integer Arithmetic
a <- 10L b <- 3L a + b # 13L (integer) a - b # 7L (integer) a * b # 30L (integer) a / b # 3.333... (numeric! division always gives numeric) a %% b # 1L (remainder — integer) a %/% b # 3L (integer division)
Division always produces a numeric result, even when both inputs are integers.
When to Use Integers
Use Integer When: Use Numeric When: ────────────────────────────────── ───────────────────────────── Counting things (rows, items) Measurements (height, weight) Index positions Prices and currency Loop counters Percentages and ratios Categorical codes (1L, 2L, 3L) Scientific values with decimals
Practical Example: Product Inventory
# Integer values make sense for counts
items_in_stock <- 250L
items_sold <- 47L
items_returned <- 3L
current_stock <- items_in_stock - items_sold + items_returned
cat("Current stock:", current_stock, "\n")
cat("Type:", class(current_stock), "\n")
Output:
Current stock: 206 Type: integer
Integer Limits
.Machine$integer.max # 2,147,483,647 (about 2.1 billion)
Integers in R can hold values up to approximately 2.1 billion. If your data exceeds this, use numeric instead. Numeric can hold values up to about 1.8 × 10^308.
For most everyday R programming, you will use numeric far more often than integer. However, understanding the integer type helps you read R code written by others and handle situations where memory efficiency or exact whole-number representation matters.
