Data Types

In the last lesson, we learned about two data types: vectors and data frames. We also learned about two different classes of vectors: numeric and factor. There are many other data types in R. Each has a special use, and to be productive in R, you need to be familiar with the major types and the operations on these types.

Primitive Types

Each R object has a un underlying “type”, which determines the set of possible values for that object. You can find the type of an object using the typeof function.

The main types include the following:

  • logical: a logical value.

    [1] TRUE
    [1] FALSE
    TRUE | FALSE  # logical 'or'
    [1] TRUE
    TRUE & FALSE  # logical 'and'
    [1] FALSE
    !TRUE  # logical 'not'
    [1] FALSE
  • integer: an integer (positive or negative). Many R programmers do not use this mode since every integer value can be represented as a double.

    1L  # suffix integers with an L to distinguish them from doubles
    [1] 1
    [1] -7
    1L:10L  # range of values
     [1]  1  2  3  4  5  6  7  8  9 10
    1:10  # (L suffix is optional)
     [1]  1  2  3  4  5  6  7  8  9 10
    7%%2  # modulo (remainder)
    [1] 1
    7%/%2  # integer division
    [1] 3
  • double: a real number stored in “double-precision floatint point format.”

    [1] 1
    [1] 3.14
    -(3 + 8/2) * 7  # arithmetic operations
    [1] -49
    2^10  # exponentiation
    [1] 1024

    A double type can store the special values Inf, -Inf, and NaN, which represent “positive infinity,” “negative infinity,” and “not a number”:

    [1] Inf
    [1] -Inf
    [1] NaN
  • complex: a complex number

    1i  # suffix with i to denote 'imaginary'
    [1] 0+1i
    [1] -4+0i
    [1] 0+1i
  • character: a sequence of characters, called a “string” in other programming languages

    "Hello, World!"  # denote a string with double quotes...
    [1] "Hello, World!"
    'abracadabra'    # ...or with single quotes (both forms are equivalent).
    [1] "abracadabra"
  • list: a list of named values (discussed in detail in the next section)

    list(a = 10, b = 11, z = "hello")
    [1] 10
    [1] 11
    [1] "hello"
  • builtin, closure, special: a function or operator (for most purposes, the distinctions between these are not important)

    [1] "builtin"
    [1] "closure"
    [1] "special"
  • NULL: a special type with only one possible value, known as NULL

    [1] "NULL"

This is not an exhaustive list, but the other modes are exotic and you probably won’t ever encounter them.

Missing Values

One unique feature of R is its support for “Not Applicable” or “Missing” values. The logical, integer, double, complex, and character types can all represent missing values, using the special constant NA.


Often, you don’t need to worry too much about the types, because R will implicitly convert between types for you. For example, consider the following sequence of commands

x <- 1:10
x[[2]] <- 3.14

When the first line gets executed, x gets created as an integer vector. In the second line, R converts x to a double vector so that it can store the value 3.14.


A “list” is a primitive type that stores a sequence of values, along with optional names for these values. The power of the list type is that it allows you to represent complicated objects.

We construct lists using the list function:

abe <- list( = "Abraham", = "Lincoln", = 180, = 76.8)

In this example, abe is a list with four elements, with names,,, and

We access the elements of a list using double square brackets. We can either specify the index of the element

[1] "Abraham"
[1] "Lincoln"

or we can specify the name

[1] "Abraham"
[1] "Lincoln"

Another way to access an element by name is to use the $ operator:

[1] 76.8
[1] 180

Both forms (abe[[""]] and abe$ are equivalent, but the $ form is more common.

As with vectors, we get the number of elements with the length function:

[1] 4

We slice lists with single square brackets:

[1] "Abraham"

[1] "Lincoln"
[1] "Abraham"

For a vector, the slice [1] is logically equivalent to the element [[1]], but for a list, these entities are distinct.

We can delete a particular element of a list by assigning it the value NULL:

abe[[""]] <- NULL

This removes the element, and shifts the indexes of subsequent elements

[1] 180
[1] 76.8


Two types we saw in the previous lesson are not primitive: data frames and factors. In fact, a data frame is a special type of list, and a factor is a special type of integer vector. These special types are known as “classes”.

Every R object is a member of one or more classes. To find these classes, use the class function:

[1] "logical"
[1] "integer"
[1] "numeric"

(Confusingly, the class for double objects is not called double; it is called numeric.)

A data frame is a list whose elements are vectors, each with the same length. A factor is an integer vector taking values in the range 1..m, with each integer corresponding to a certain level. R distinguishes between these types and their underlying representations by assigning them to different classes.

bikedata <- read.csv("bikedata.csv")
[1] "list"
[1] "data.frame"
[1] "integer"
[1] "factor"

The power of classes is that they allow you to change how certain functions behave. Compare the following two otputs:

Black  Blue Green  Grey Other   Red White  NA's 
  262   636   149   531    52   378   333    14 
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
   1.00    2.00    4.00    3.83    6.00    7.00      14 

Here, unclass is a function that converts to the underlying primitive type. When we summarize an object with class factor, we report counts for the levels; when we summarize an object with class integer, we report quartiles and other statistics.

Advanced R programmers create new kinds of classes, along with specialized functions to act on these classes.