R's Type System Overview
Understand double, integer, character, logical, and complex types.
R's Type System Overview is a free R Academy lesson on CoddyKit. This is lesson 1 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.
R's Type System
R has a rich type system. Every object has a class (high-level category) and a type (low-level storage mode). Understanding both helps you write correct, efficient code and debug type-related errors.
# Check class and type of various objects
cat('class(42L) :', class(42L), '
')
cat('typeof(42L) :', typeof(42L), '
')
cat('class(3.14) :', class(3.14), '
')
cat('typeof(3.14) :', typeof(3.14), '
')
cat('class(TRUE) :', class(TRUE), '
')
cat('class("hello"):', class('hello'), '
')class() vs typeof()
class(x) returns the high-level category used by R's object-oriented system (e.g., 'integer', 'numeric', 'matrix'). typeof(x) returns the low-level C storage type (e.g., 'integer', 'double', 'closure').
m <- matrix(1:4, nrow = 2)
cat('class(matrix) :', class(m), '
') # matrix array
cat('typeof(matrix) :', typeof(m), '
') # integer
f <- factor(c('a', 'b', 'a'))
cat('class(factor) :', class(f), '
') # factor
cat('typeof(factor) :', typeof(f), '
') # integer (stored as int!)
cat('class(list) :', class(list()), '
') # list
cat('typeof(list) :', typeof(list()), '
') # listis.numeric() — Numeric Check
is.numeric(x) returns TRUE for both integer and double values (and numeric matrices). It checks the numeric supertype, not the specific storage mode.
cat('is.numeric(3.14):', is.numeric(3.14), '
') # TRUE (double)
cat('is.numeric(3L): ', is.numeric(3L), '
') # TRUE (integer)
cat('is.numeric(TRUE):', is.numeric(TRUE), '
') # FALSE
cat('is.numeric("3"): ', is.numeric('3'), '
') # FALSE
cat('is.numeric(NA): ', is.numeric(NA), '
') # FALSE (NA is logical)is.integer() vs is.double()
is.integer() tests specifically for integer storage (created with L suffix or as.integer()). is.double() tests for floating-point (double) storage. Both are subsets of numeric.
x_int <- 5L
x_dbl <- 5.0
cat('--- x_int = 5L ---
')
cat('is.integer:', is.integer(x_int), '
') # TRUE
cat('is.double: ', is.double(x_int), '
') # FALSE
cat('is.numeric:', is.numeric(x_int), '
') # TRUE
cat('--- x_dbl = 5.0 ---
')
cat('is.integer:', is.integer(x_dbl), '
') # FALSE
cat('is.double: ', is.double(x_dbl), '
') # TRUE
cat('is.numeric:', is.numeric(x_dbl), '
') # TRUEis.character() — String Check
is.character(x) returns TRUE if x is a character (string) vector. Use this to validate inputs that should be text, or to distinguish numeric from character NA types.
greetings <- c('hello', 'world', 'R')
numbers <- c(1, 2, 3)
numeric_string <- '42'
cat('is.character(greetings): ', is.character(greetings), '
')
cat('is.character(numbers): ', is.character(numbers), '
')
cat('is.character(numeric_string): ', is.character(numeric_string), '
')
cat('is.character(NA_character_): ', is.character(NA_character_), '
')is.logical() — Boolean Check
is.logical(x) returns TRUE if x contains logical (Boolean) values. Note that the untyped NA is a logical value by default.
flags <- c(TRUE, FALSE, TRUE, NA)
cat('is.logical(flags): ', is.logical(flags), '
') # TRUE
cat('is.logical(NA): ', is.logical(NA), '
') # TRUE (default NA is logical)
cat('is.logical(1): ', is.logical(1), '
') # FALSE
cat('is.logical("TRUE"):', is.logical('TRUE'), '
') # FALSE
cat('typeof(NA): ', typeof(NA), '
') # logicalR's Type Hierarchy
R's atomic types form a hierarchy from simplest to most complex: logical < integer < double < complex < character. When mixing types in a vector, R coerces all elements to the most complex type present.
# Type hierarchy in action
cat('c(TRUE, 1L) type: ', typeof(c(TRUE, 1L)), '
') # integer
cat('c(1L, 1.5) type: ', typeof(c(1L, 1.5)), '
') # double
cat('c(1.5, 1+0i) type: ', typeof(c(1.5, 1+0i)), '
') # complex
cat('c(1+0i, "a") type: ', typeof(c(1+0i, 'a')), '
') # character
cat('c(TRUE, "x") type: ', typeof(c(TRUE, 'x')), '
') # characterInspecting Types Programmatically
You can write type-inspection functions to audit a list of objects. Combining class(), typeof(), and length() gives a comprehensive summary of any object's structure.
describe_type <- function(x, label) {
cat(label, ': class =', class(x), ', typeof =', typeof(x),
', length =', length(x), '
')
}
describe_type(42L, 'integer literal')
describe_type(3.14, 'double literal')
describe_type(TRUE, 'logical')
describe_type('hello', 'character')
describe_type(c(1,2,3), 'numeric vector')
describe_type(list(1,'a'), 'list')storage.mode() and mode()
storage.mode(x) is similar to typeof(x) but uses the S3 naming convention (e.g., returns 'double' instead of 'double'). mode(x) returns a slightly coarser grouping that lumps integer and double together as 'numeric'.
x_int <- 5L
x_dbl <- 5.0
cat('mode(5L): ', mode(x_int), '
') # numeric
cat('mode(5.0): ', mode(x_dbl), '
') # numeric
cat('storage.mode(5L): ', storage.mode(x_int), '
') # integer
cat('storage.mode(5.0):', storage.mode(x_dbl), '
') # double
# mode() lumps int+dbl; storage.mode() / typeof() distinguish themType Inspection for Lists and Data Frames
For more complex objects like lists and data frames, sapply(df, class) applies class() to each column, giving a named vector of column types — a quick way to audit a data frame's schema.
df <- data.frame(
id = 1:3,
name = c('Alice', 'Bob', 'Carol'),
score = c(88.5, 79.0, 92.3),
passed = c(TRUE, TRUE, TRUE)
)
cat('Column types:
')
print(sapply(df, class))
cat('Column typeof:
')
print(sapply(df, typeof))Type System Summary
Here is a quick reference for R's type inspection tools:
class(x)— high-level S3 class nametypeof(x)— low-level C storage typeis.numeric(x)— TRUE for integer or doubleis.integer(x)— TRUE for integer storage onlyis.double(x)— TRUE for double (floating point) onlyis.character(x)— TRUE for stringsis.logical(x)— TRUE for TRUE/FALSE/NA
x <- 42L
cat('class(42L): ', class(x), '
')
cat('typeof(42L): ', typeof(x), '
')
cat('is.numeric(42L): ', is.numeric(x), '
')
cat('is.integer(42L): ', is.integer(x), '
')
cat('is.double(42L): ', is.double(x), '
')
cat('is.character(42L):', is.character(x), '
')Quick Check
What does is.numeric(5L) return in R?
Recap: R's Type System
Great work! Key takeaways from this lesson:
class(x)returns the high-level type;typeof(x)returns the low-level storage typeis.numeric()is TRUE for both integers and doublesis.integer()andis.double()distinguish the two numeric subtypes- R's type hierarchy (low → high): logical → integer → double → complex → character
- Mixing types in a vector triggers automatic coercion to the highest type
sapply(df, class)audits all column types in a data frame
# Type audit function
audit_types <- function(x, name = 'x') {
cat(name, ': class=', class(x), ', typeof=', typeof(x),
', is.numeric=', is.numeric(x), '
')
}
audit_types(1L, 'integer')
audit_types(1.0, 'double')
audit_types(TRUE, 'logical')
audit_types('hello', 'character')
audit_types(1+2i, 'complex')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 “R's Type System Overview” lesson free?
Yes — any 3 lessons of the R Academy learning path, including “R's Type System Overview”, 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 “R's Type System Overview”?
Understand double, integer, character, logical, and complex types. 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 1 of 4.
How long does the “R's Type System Overview” 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