Data Modes and Classes in R

In R, data modes and classes define the fundamental attributes and behavior of a data object.  For example, different modes and classes are handled differently by core functions like print(), summary(), and plot().

Data Object Modes

All data in R is an object and all objects have a “mode.”  The mode determines what type of information can be found within the object and how that information is stored.  Atomic “modes” are the basic building blocks for data objects in R.  There are 6 basic atomic modes:

Data ModeStorageExample
logicallogicalTRUE or FALSE
numericinteger, single or doubleFloating point real numbers; 3, o.753, 1.e+200
complexcomplex3 + 1.23i
charactercharacterstrings in quotes (“) or apostrophes (‘)
functionspecial or <- function(x) {…}
namesymbolany name assigned to an object (e.g. "")

The atomic mode and the storage mode of any data object can be obtained as follows:

Data Object Classes

R has an elaborate class system.  If the number of modes is limited, the number of object classes is numerous and custom classes can be created.  The class of an object is used to support object-oriented programming in R. Specifically, the class defines how an object will be treated by generic functions like print()plot()summary(), etc.

Important classes, including custom classes from popular packages, are listed below:

  • Vector data is classed in R using its mode as either numericcharacter, etc.
  • Rectangular data is classed in R as either a matrixdata frame or list object;
  • Sourced scripts assume the class function;
  • Categorical information are classed as factor objects;
  • Time/date information in R is classed as a timeDate object; and
  • Spatial GIS data can assume a wide range of classes, including SpatialPointsSpatialLinesSpatialPolygons, and more.

The class of any object can be interrogated and quickly determined using the class() function.  The example below creates a simple matrix and obtains its class:

At the same time, the attributes() functions can be used to identify other attributes of an object.  The attr() function does the same, but supports additional arguments for more precise diagnostics.

Back | Next

Leave a Reply