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:
[table id=1 /]
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 numeric, character, etc.
- Rectangular data is classed in R as either a matrix, data 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 SpatialPoints, SpatialLines, SpatialPolygons, 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:
> x <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol=3)
[,1] [,2] [,3]
[1,] 1 3 12
[2,] 2 11 13
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.