0Pricing
R Academy · Lesson

R's Type System Overview

Understand double, integer, character, logical, and complex types.

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()), '
') # list

All lessons in this course

  1. R's Type System Overview
  2. Converting Between Numeric Types
  3. Logical and Character Conversion
  4. Coercion Pitfalls and Best Practices
← Back to R Academy