01 / 20
1L "1"

Type Coercion
in R

Learn how R automatically converts data types, the hidden rules that govern these transformations, and the common surprises that catch programmers off guard.

R Programming

What is Type Coercion?

In R, every value has a type (also called a "mode" or "class"): logical, integer, double, character, and more. When you combine values of different types, R must decide on a single type for the result.

Type coercion is the process where R automatically converts one type into another so that operations can proceed. This happens silently: no warning, no error, just a quiet transformation behind the scenes.

R's Four Atomic Types

logical
TRUE or FALSE: the simplest type, representing Boolean values
integer
Whole numbers written with an L suffix: 1L, 42L, -7L
double
Decimal numbers (R's default numeric type): 3.14, 2.0, -1.5
character
Text strings enclosed in quotes: "hello", "42", "TRUE"

Implicit vs Explicit Coercion

Implicit (Automatic)
R silently converts types when you mix them in vectors, arithmetic, or comparisons. You never ask for it: it just happens.
Explicit (Manual)
You deliberately convert using functions like as.numeric(), as.character(), or as.logical(). You are in control.
# Implicit: R decides c(1L, 2.5) # [1] 1.0 2.5 (integer → double) # Explicit: You decide as.integer(2.5) # [1] 2 (double → integer, truncated)

The Coercion Hierarchy

logical integer double character least general most general

How Logicals Become Numbers

One of the most useful coercion rules: TRUE becomes 1 and FALSE becomes 0. This lets you use logical vectors in arithmetic, which is a core R idiom.

# TRUE → 1, FALSE → 0 TRUE + TRUE # [1] 2 sum(c(TRUE, FALSE, TRUE)) # [1] 2 (counts TRUEs) mean(c(TRUE, FALSE, TRUE)) # [1] 0.6667 (proportion of TRUEs)

Vectors vs Functions: Where Coercion Happens

In Vectors (c()) In Functions
Trigger Mixing types in c() Passing wrong type to argument
Rule Hierarchy: most general wins Function-specific behavior
Warning? Never: always silent Sometimes: NAs introduced
Example c(1, "a") → all character as.numeric("x") → NA + warning

How to Predict Coercion Results

1
Identify all types present. Use class() or typeof() on each value to confirm what you're working with.
2
Find the most general type using the hierarchy: logical → integer → double → character.
3
Convert all values to that type. TRUE → 1 → 1.0 → "1". FALSE → 0 → 0.0 → "0".
4
Check for surprises: numbers become their string representation ("3.14"), not their meaning. "TRUE" and TRUE are very different.

Example 1: Mixing in c()

What happens when we combine a logical, an integer, and a character in a single vector?

x <- c(TRUE, 42L, "hello") x # [1] "TRUE" "42" "hello" class(x) # [1] "character"
TRUE logical 42L integer "hello" character = "TRUE" "42" "hello" all character

Example 2: Arithmetic with Logicals

Arithmetic operators force logical values to become numbers. This enables a powerful counting pattern.

scores <- c(85, 42, 91, 67, 73) # How many scores are above 70? sum(scores > 70) # [1] 3 # What proportion passed? mean(scores > 70) # [1] 0.6

Example 3: Dangerous Coercion Surprises

Not all coercions are friendly. When R cannot convert a value meaningfully, it produces NA and sometimes a warning.

# Explicit coercion failure as.numeric("hello") # [1] NA # Warning: NAs introduced by coercion # Silent surprise in c() c(1, 2, "3") # [1] "1" "2" "3" ← all character, no warning! # Numeric operations on the result fail sum(c(1, 2, "3")) # Error: invalid 'type' (character) of argument

How NA Interacts with Coercion

NA (Not Available) is R's missing value. It is special: NA can exist in any type, and it coerces along with everything else.

# NA adapts to the vector's type c(1, 2, NA) # [1] 1 2 NA ← numeric NA c("a", NA) # [1] "a" NA ← character NA # Arithmetic with NA always returns NA 5 + NA # [1] NA is.na(NA) # [1] TRUE

Explicit Coercion Functions

as.numeric()
Converts to double: "3.14" → 3.14, TRUE → 1, "text" → NA
as.integer()
Converts to integer (truncates): 3.9 → 3L, TRUE → 1L
as.character()
Converts to string: 42 → "42", TRUE → "TRUE"
as.logical()
Converts to logical: 0 → FALSE, non-zero → TRUE, "TRUE" → TRUE

Common Mistakes with Coercion

✕ String Number Trap
Assuming c(1, 2, "3") is numeric. The "3" forces everything to character: no math possible.
✕ Factor Surprise
Using as.numeric() on a factor returns internal codes, not the displayed values. Use as.numeric(as.character(f)) instead.
✕ Ignoring NA Warnings
as.numeric("abc") returns NA with a warning. If you ignore the warning, downstream calculations can fail or produce wrong results.

Coercion Quick Reference

From → To numeric character logical
TRUE 1 "TRUE" TRUE
FALSE 0 "FALSE" FALSE
42L 42 "42" TRUE
3.14 3.14 "3.14" TRUE
0 0 "0" FALSE
"hello" NA "hello" NA
"1" 1 "1" NA

Question 1 of 5

What is the result of c(TRUE, 3L, 2.5) in R?

Question 2 of 5

What does sum(c(TRUE, FALSE, TRUE, TRUE)) return?

Question 3 of 5

What is the output of as.logical(0)?

Question 4 of 5

In R's coercion hierarchy, which type is the most general (i.e., all other types coerce toward it)?

Question 5 of 5

What does as.numeric("hello") return?