12 Base types

12.1 Introduction

To talk about objects and OOP in R we first need to clear up a fundamental confusion about two uses of the word “object”. So far in this book, we’ve used the word in the general sense captured by John Chambers’ pithy quote: “Everything that exists in R is an object”. However, while everything is an object, not everything is object-oriented. This confusion arises because the base objects come from S, and were developed before anyone thought that S might need an OOP system. The tools and nomenclature evolved organically over many years without a single guiding principle.

Most of the time, the distinction between objects and object-oriented objects is not important. But here we need to get into the nitty gritty details so we’ll use the terms base objects and OO objects to distinguish them.


  • Section 12.2 shows you how to identify base and OO objects.

  • Section 12.3 gives a complete set of the base types used to build all objects.

12.2 Base versus OO objects

To tell the difference between a base and OO object, use is.object() or sloop::otype():

Technically, the difference between base and OO objects is that OO objects have a “class” attribute:

You may already be familiar with the class() function. This function is safe to apply to S3 and S4 objects, but it returns misleading results when applied to base objects. It’s safer to use sloop::s3_class(), which returns the implicit class that the S3 and S4 systems will use to pick methods. You’ll learn more about s3_class() in Section 13.7.1.

12.3 Base types

While only OO objects have a class attribute, every object has a base type:

Base types do not form an OOP system because functions that behave differently for different base types are primarily written in C code that uses switch statements. This means that only R-core can create new types, and creating a new type is a lot of work because every switch statement needs to be modified to handle a new case. As a consequence, new base types are rarely added. The most recent change, in 2011, added two exotic types that you never see in R itself, but are needed for diagnosing memory problems. Prior to that, the last type added was a special base type for S4 objects added in 2005.

In total, there are 25 different base types. They are listed below, loosely grouped according to where they’re discussed in this book. These types are most important in C code, so you’ll often see them called by their C type names. I’ve included those in parentheses.

You may have heard of mode() and storage.mode(). Do not use these functions: they exist only to provide type names that are compatible with S.

12.3.1 Numeric type

Be careful when talking about the numeric type, because R uses “numeric” to mean three slightly different things:

  1. In some places numeric is used as an alias for the double type. For example as.numeric() is identical to as.double(), and numeric() is identical to double().

    (R also occasionally uses real instead of double; NA_real_ is the one place that you’re likely to encounter this in practice.)

  2. In the S3 and S4 systems, numeric is used as a shorthand for either integer or double type, and is used when picking methods:

  3. is.numeric() tests for objects that behave like numbers. For example, factors have type “integer” but don’t behave like numbers (i.e. it doesn’t make sense to take the mean of factor).

In this book, I consistently use numeric to mean an object of type integer or double.