Desktop Survival Guide
by Graham Williams

Data Types

The basic types of objects in R include logical, integer, double, complex, and character. We obtain the type of an object using the typeof function:

> typeof(TRUE)
[1] "logical"
> typeof(5+4)
[1] "double"
> typeof(letters)
[1] "character"
> typeof(typeof)
[1] "closure"

The last example here illustrates the fact that functions in R are also first class objects--that is, they are just like any other data type. Functions have a type called a closure, which is a type that wraps up the function's definition and the environment in which the function was created. The other special types include NULL, symbol, environment, promise, language, special, builtin, expression, and list.

The mode of an object is another indication of the type of the object. For example, objects of type integer and double are of mode numeric.

> mode(x)
> storage.mode(x)
> class(x)                    	  # The object class for method dispatch

Normally we build up an object to have some structure, like a vector, an array, a matrix, or a data frame. The basic building block of a data structure in R is a vector of objects of some type. Indeed, an object of type double will in fact be a vector of numbers.

R will keep track of objects you have created:

  > ls()		  # List all objects you have created.
  > rm(list=ls(all=TRUE)) # Remove all of your objects.

In the following sections we introduce each of the main types of objects and illustrate typical operations for manipulating the objects.

Copyright © Togaware Pty Ltd
Support further development through the purchase of the PDF version of the book.
The PDF version is a formatted comprehensive draft book (with over 800 pages).
Brought to you by Togaware. This page generated: Sunday, 22 August 2010