Data Types & Classes
A data type is the kind of value a piece of R data holds — numeric, integer, character, or logical — and it determines what operations you can perform on it.
Learn Data Types & Classes in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll learn to identify every basic type with class() and typeof(), test types with the is.*() family, convert between them with as.*(), and understand R's coercion rules plus the crucial difference between NA and NULL.
What You'll Learn in This Lesson
1️⃣ The Four Core Types
Almost everything in R is built from four atomic types. A numeric (also called a double) is any number and is the default — even 5 is numeric. An integer is a whole number you create with an L suffix, like 42L . A character is text wrapped in quotes, and a logical is TRUE or FALSE .
2️⃣ Inspecting Types: class(), typeof(), is.*()
Three tools answer three different questions. class() gives the conceptual type you usually reason about. typeof() reveals R's internal storage — note that a plain number is stored as a "double" even though its class is "numeric" . The is.*() functions return TRUE / FALSE , perfect for an if .
3️⃣ Coercion: Converting Between Types
You convert on purpose with the as.*() family. Watch the gotchas: as.integer(3.9) truncates to 3 (it does not round), and as.logical(0) is FALSE while any non-zero number is TRUE . R also coerces automatically when you mix types in one vector, promoting everything to the most flexible type: logical < integer < numeric < character.
The mental model: when types collide, R picks the type that can represent all the values without losing information — and character can represent anything, so it always wins.
4️⃣ NA vs NULL
NA means a value is missing — the slot exists but the data is unknown, so it still counts toward length() . NULL means a value is absent — there is no slot at all, so c(1, NULL, 3) drops it and leaves length 2. Use is.na() to find missing values (you'll go deep on this in the Missing Data lesson).
Your turn. Fill in the ___ blanks, run it, and compare with the expected output.
Write it from the outline, run it, and predict each result before you see it. The goal is to internalise the coercion hierarchy so it never surprises you again.
📋 Quick Reference — Types
Practice quiz
What class does the literal 5 have by default in R?
- integer
- character
- numeric
- logical
Answer: numeric. Plain numbers are numeric (doubles) unless you add the L suffix.
How do you create a true integer in R?
- Add an L suffix, e.g. 42L
- Wrap it in quotes, e.g. "42"
- Prefix with i, e.g. i42
- Use 42.0
Answer: Add an L suffix, e.g. 42L. The L suffix forces integer storage; 42 alone is numeric.
What does typeof(3.14) return?
- "numeric"
- "float"
- "integer"
- "double"
Answer: "double". Numeric values are stored as doubles, so typeof gives "double".
What does as.integer(3.9) produce?
- 4 (it rounds)
- 3 (it truncates)
- 3.9
- An error
Answer: 3 (it truncates). as.integer() truncates toward zero; use round() first to round.
What does c(1, TRUE, "x") coerce all elements to?
- character
- numeric
- logical
- list
Answer: character. A vector holds one type; character is the most flexible, so all become character.
What is the coercion hierarchy from least to most flexible?
- character < numeric < integer < logical
- numeric < logical < character < integer
- logical < integer < numeric < character
- integer < character < numeric < logical
Answer: logical < integer < numeric < character. When types mix, R promotes toward character, the most general type.
What does as.logical(0) return?
- TRUE
- FALSE
- 0
- NA
Answer: FALSE. 0 coerces to FALSE; any non-zero number coerces to TRUE.
How does NA differ from NULL?
- They are identical
- NULL counts toward length but NA does not
- NA only appears in characters
- NA occupies a slot and counts toward length; NULL is dropped
Answer: NA occupies a slot and counts toward length; NULL is dropped. NA is a present-but-missing value; NULL is the absence of a value.
What does length(c(1, NA, 3)) return?
- 2
- 3
- 1
- 0
Answer: 3. NA still occupies an element, so the length is 3.
Which function returns TRUE/FALSE for each missing value in a vector?
- na.rm(x)
- missing(x)
- is.na(x)
- is.null(x)
Answer: is.na(x). is.na() tests each element for being NA, returning a logical vector.