HOWTO · R
How to Check Variable Data Types in R
Learn when to use typeof(), class(), str(), type predicates, and sapply() to inspect R objects and data-frame columns.
On this page
To check a variable in R, choose a function based on what you need to know: typeof() reports its underlying storage type, class() reports its class, str() summarizes an object’s structure, and predicates such as is.numeric() return TRUE or FALSE. These functions inspect an object; they do not change its type.
Choose between typeof() and class() for one R object
Use typeof() when you need the basic storage type R uses for an object. Use class() when you need the class that affects how generic functions handle it. The distinction matters because an object’s storage type and class can differ. For example, a factor is stored as integer codes but has class "factor".
Run this example:
scores <- factor(c("low", "high", "low"))
print(typeof(scores))
print(class(scores))
The two calls inspect the same object from different perspectives. typeof() reports its underlying representation; class() reports the factor class that gives the values their categorical behavior. For common numeric and character vectors, the results may look similar, but they answer different questions.
Inspect every data-frame column with str()
Use str() for a quick structural overview of a data frame. It lists the number of rows and columns and summarizes each column, including its storage type and a few example values. This is useful before choosing transformations or type checks for individual columns. The function prints a summary and leaves the data frame unchanged.
Run this example:
people <- data.frame(
name = c("Ari", "Bo"),
visits = c(3L, 5L),
active = c(TRUE, FALSE)
)
str(people)
Look for the column names and type abbreviations in the summary: character values appear as chr, integer values as int, and logical values as logi. R may store ordinary whole-number values as num; use an integer literal such as 3L when integer storage is important.
Test whether an object or column has a specific type
Type predicates answer a yes-or-no question about an object. For example, is.numeric() tests whether an object is numeric, is.character() tests for character data, and is.integer() tests specifically for integer storage. Each returns an R logical value: TRUE or FALSE. A numeric value such as 3 is usually stored as double, so is.integer(3) is FALSE; write 3L to create an integer literal.
Run this example:
value <- 3L
labels <- c("new", "returning")
people <- data.frame(visits = c(3, 5), name = c("Ari", "Bo"))
print(is.integer(value))
print(is.character(labels))
print(is.numeric(people$visits))
print(is.character(people$name))
Use $ or [[ ]] to pass a data-frame column to a predicate. The answer applies to the selected column as a whole; it does not check every cell for a different type. A predicate can also return FALSE when the object’s storage or class does not meet the function’s specific definition, even if its values look similar to the requested type.
Apply a type function across elements or columns with sapply()
Use sapply() to apply a function to each item in a collection and simplify the results when possible. For a data frame, iteration is over columns, so applying typeof() reports one storage type per column. For an atomic vector, R generally coerces all elements to a common type; applying a type function to each element therefore cannot recover distinct original types that coercion already removed.
Run this example:
people <- data.frame(
name = c("Ari", "Bo"),
visits = c(3L, 5L),
active = c(TRUE, FALSE)
)
column_types <- sapply(people, typeof)
print(column_types)
Here sapply() calls typeof() once for each column and returns the results in a named vector. Use a type predicate instead when the question is whether each column meets a condition, for example sapply(people, is.character). For complicated or mixed collections, inspect the structure with str() or iterate explicitly so the result’s shape is clear.
Use typeof() for an object’s storage type, class() for its class, str() for a structural overview, predicates for yes-or-no checks, and sapply() to apply a check across columns. The supplied runtime record contains observed results for all four examples.