In R, it's important to check the type of variables to understand how data is stored and how it behaves. R provides several functions to test the type or class of a variable.
class(x)
β Returns the class/type of object x
.typeof(x)
β Returns the internal storage mode of x
.is.numeric(x)
β Checks if x
is numeric.is.integer(x)
β Checks if x
is an integer.is.character(x)
β Checks if x
is a character string.is.logical(x)
β Checks if x
is logical (TRUE/FALSE).a <- 10 b <- 10L c <- "R language" d <- TRUE print(class(a)) # "numeric" print(typeof(a)) # "double" print(is.numeric(a)) # TRUE print(is.integer(a)) # FALSE print(class(b)) # "integer" print(is.integer(b)) # TRUE print(class(c)) # "character" print(is.character(c)) # TRUE print(class(d)) # "logical" print(is.logical(d)) # TRUE
Type checking helps avoid errors by ensuring functions receive the expected data types. Itβs also useful when debugging and for conditional programming based on data types.
π‘ Quick Tip:
Use class()
for general type info, but typeof()
gives the underlying storage type. Use specific is.*()
functions for clear boolean checks.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!