Converting Between Numeric Types
Use as.integer(), as.double(), and as.numeric() safely.
Converting Between Numeric Types is a free R Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the R Academy learning path, and your progress syncs across the web and the CoddyKit app. The R Academy course includes 4 lessons in total.
Integer vs Double in R
R has two numeric types: integer (whole numbers, created with the L suffix) and double (floating-point, the default for numeric literals). Most of the time R uses double, but distinguishing them matters for type safety and memory efficiency.
x_dbl <- 5 # double by default
x_int <- 5L # integer (L suffix)
cat('typeof(5): ', typeof(x_dbl), '
') # double
cat('typeof(5L): ', typeof(x_int), '
') # integer
cat('5 == 5L: ', x_dbl == x_int, '
') # TRUE (values equal)
cat('identical: ', identical(x_dbl, x_int), '
') # FALSE (types differ)as.integer() — Convert to Integer
as.integer(x) converts a value to integer by truncating (not rounding) the decimal part. This means 3.9 becomes 3, not 4. The L suffix is syntactic sugar for integer literals.
cat('as.integer(3.7): ', as.integer(3.7), '
') # 3 (truncated, not 4)
cat('as.integer(3.2): ', as.integer(3.2), '
') # 3
cat('as.integer(-2.9): ', as.integer(-2.9), '
') # -2 (toward zero)
cat('as.integer(10.9): ', as.integer(10.9), '
') # 10
# Compare with round
cat('round(3.7): ', round(3.7), '
') # 4as.double() and as.numeric()
as.double(x) and as.numeric(x) are equivalent — both convert to a double (floating-point) value. Use them to convert integers to doubles when you need fractional precision.
count <- 7L # integer
cat('typeof(count): ', typeof(count), '
')
dbl_count <- as.double(count)
cat('typeof(as.double(7L)): ', typeof(dbl_count), '
') # double
cat('Value: ', dbl_count, '
') # 7
# Division automatically promotes to double
cat('7L / 2L: ', 7L / 2L, '
') # 3.5 (double)
cat('typeof(7L / 2L): ', typeof(7L / 2L), '
') # doubleInteger Arithmetic
Integer arithmetic in R produces integers for +, -, and *, but always returns double for /. This can cause surprise if you expect integer division to stay integer.
a <- 6L
b <- 3L
cat('6L + 3L =', a + b, '| typeof:', typeof(a + b), '
') # 9 integer
cat('6L - 3L =', a - b, '| typeof:', typeof(a - b), '
') # 3 integer
cat('6L * 3L =', a * b, '| typeof:', typeof(a * b), '
') # 18 integer
cat('6L / 3L =', a / b, '| typeof:', typeof(a / b), '
') # 2 DOUBLE!
cat('6L %/% 3L =', a %/% b, '| typeof:', typeof(a %/% b), '
') # 2 integerTruncation vs Rounding
A critical distinction: as.integer() truncates (drops decimals), while round() rounds to the nearest whole number. Always be explicit about which behaviour you intend.
values <- c(2.1, 2.5, 2.9, -2.1, -2.5, -2.9)
cat('Original: ', values, '
')
cat('as.integer(): ', as.integer(values), '
') # truncate toward 0
cat('round(): ', round(values), '
') # banker rounding
cat('floor(): ', floor(values), '
') # round down
cat('ceiling(): ', ceiling(values), '
') # round upInteger Overflow Risk
Integers in R are 32-bit and can hold values up to approximately ±2.1 billion. Exceeding this range causes an NA with a warning. Doubles can hold much larger values (up to ~1.8 × 10^308).
max_int <- .Machine$integer.max
cat('Max integer:', max_int, '
') # 2147483647
# Overflow produces NA
cat('max_int + 1L:', max_int + 1L, '
') # NA with warning
# Double handles it fine
cat('as.double(max_int) + 1:', as.double(max_int) + 1, '
') # 2147483648The L Suffix in Vectors
You can create integer vectors using the L suffix on each element, or by passing a sequence through as.integer(). The colon operator : automatically produces integers.
# Colon produces integers
seq_int <- 1:5
cat('1:5 typeof:', typeof(seq_int), '
') # integer
# Explicit integer vector
vec_int <- c(10L, 20L, 30L, 40L)
cat('c(10L,...) typeof:', typeof(vec_int), '
') # integer
# as.integer on a vector
vec_dbl <- c(1.1, 2.9, 3.5, 4.7)
cat('as.integer(c(1.1,...)):', as.integer(vec_dbl), '
') # 1 2 3 4Memory and Performance
Integers use 4 bytes per element; doubles use 8 bytes. For very large vectors of whole numbers, using integers can halve memory use. However, arithmetic between mixed types automatically promotes to double.
n <- 1000000L
vec_int <- 1:n # integer: 4 bytes each
vec_dbl <- as.double(1:n) # double: 8 bytes each
cat('Integer vector size:', object.size(vec_int), 'bytes
')
cat('Double vector size: ', object.size(vec_dbl), 'bytes
')
cat('Ratio:', as.numeric(object.size(vec_dbl)) / as.numeric(object.size(vec_int)), '
')Checking Integer Storage with is.integer()
Always use is.integer() (not is.numeric()) when you specifically need to verify integer storage. A number without the L suffix is double even if it looks like a whole number.
a <- 5 # looks like integer, but it is double
b <- 5L # true integer
cat('is.integer(5): ', is.integer(a), '
') # FALSE!
cat('is.integer(5L): ', is.integer(b), '
') # TRUE
cat('is.numeric(5): ', is.numeric(a), '
') # TRUE
cat('is.numeric(5L): ', is.numeric(b), '
') # TRUE
cat('class(5): ', class(a), '
') # numeric
cat('class(5L): ', class(b), '
') # integerConversion Workflow Example
A practical workflow: read data with doubles (the default), compute, then explicitly convert results to integers where whole numbers are required (e.g., counts, indices, IDs).
# Simulated: reading counts as doubles (common from CSV)
counts_raw <- c(12.0, 8.0, 15.0, 3.0, 22.0)
cat('typeof raw:', typeof(counts_raw), '
') # double
# Convert to integer for proper storage
counts_int <- as.integer(counts_raw)
cat('typeof converted:', typeof(counts_int), '
') # integer
cat('Counts:', counts_int, '
')
cat('Total:', sum(counts_int), '
')Numeric Conversion Summary
Key conversion functions for numeric types:
as.integer(x)— truncate to integer (drops decimals, toward zero)as.double(x)/as.numeric(x)— convert to doubleround(x)— round to nearest (use for integer-like values)floor(x)/ceiling(x)— round down / up5Lsyntax — integer literal (no conversion needed)1:n— always produces integers
x <- 7.8
cat('as.integer(7.8):', as.integer(x), '
') # 7 (truncate)
cat('round(7.8): ', round(x), '
') # 8
cat('floor(7.8): ', floor(x), '
') # 7
cat('ceiling(7.8): ', ceiling(x), '
') # 8Quick Check
What does as.integer(3.9) return in R?
Recap: Numeric Type Conversion
Excellent! Key takeaways from this lesson:
- R has two numeric types: integer (L suffix, 4 bytes) and double (default, 8 bytes)
as.integer(x)truncates toward zero — not roundingas.double()andas.numeric()are identical — convert to double/always returns double, even for integer operands1:ncreates integer sequences; numeric literals without L are double- Integer overflow at ~2.1 billion → use double for very large whole numbers
# Demonstrate truncation vs rounding
test_vals <- c(0.1, 0.5, 0.9, 1.5, 2.5, 3.7)
cat('Values: ', test_vals, '
')
cat('as.integer(): ', as.integer(test_vals), '
') # truncation
cat('round(): ', round(test_vals), '
') # nearest (banker)Learn R with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 43
- Lessons
- 159
Frequently Asked Questions
Is the “Converting Between Numeric Types” lesson free?
Yes — any 3 lessons of the R Academy learning path, including “Converting Between Numeric Types”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The R Academy course includes 4 lessons in total.
What will I learn in “Converting Between Numeric Types”?
Use as.integer(), as.double(), and as.numeric() safely. You practise R Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start R Academy?
No prior experience is required. R Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “Converting Between Numeric Types” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this R Academy lesson?
Yes. Every R Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- R's Type System Overview
- Converting Between Numeric Types
- Logical and Character Conversion
- Coercion Pitfalls and Best Practices