Objects, Objects, uhm Vectors

We covered about half of Chapter 3 of An Introduction to R. In what follows a review of the key concepts that were introduced.

Objects everywhere

We began with the notion that everything in R is essentially expressed in terms of objects. R is an object oriented language and even functions (e.g., round(), print()) are objects.

The most obvious objects we deal with are data structures. In R we can distinguish 5 data structures. Vectors, matrices, and arrays are homogeneous data structures; lists and data frames are heterogeneous structures.

  homogeneous heterogeneous
1-dimensional vectors lists
2-dimensional matrices data frames
n-dimensional arrays  

The elements of homogeneous data structures have to be of the same type, whereas data of different types can be “stored” in heterogeneous objects.

R can represent data of four types:

  1. integer
  2. double
  3. character (strings)
  4. logical

We encountered some of these types before:

TRUE # is a logical object (as is FALSE)

193 # is an object of type double (as is -13.4 and pi)

"Hello" # is an object of type character (as is "Help!" or "123")

18L # is an object of type integer (as is -9L)

We can ask R to tell us what type of object we are dealing with by using the typeof() function:

typeof(x = TRUE) # evaluates to "logical"

typeof(x = 193) # evaluates to "double"

typeof(x = "Hello") # evaluates to "character"

typeof(x = 18L) # evaluates to "integer"

Notice that the output of the the typeof() function is an object itself. It is of type character.


The second big concept we discussed was assignment of various data to arbitrary names. If it helps you to make sense of assignment, think of it as the storing of data or elements of various types into vessels or containers. To assign values or elements or really objects to names we use the assignment operator: <-.

We can store the number 25.02 (i.e., data of type double) into a named container (into a data structure called vector) like so:

MyContainer <- 25.02   

This above line of code assigns the value 25.02 to the name MyContainer. 25.02 in some sense is stored inside of MyContainer.

Similarly we could store other types of objects – say the character type element: “Hello”.

Greeting <- "Hello"

typeof(Greeting) # will evaluate to "character"


What we have done here implicitly is worked with and created vectors. Vectors – or technically atomic vectors – are objects or data-structures containing data elements of the same type.

12 # is a vector (a named data structure containing the double 12)

"Angry!" # is a vector

Mood <- "Angry!" # creates a named vector called Mood
                 # Mood contains the character object "Angry!"
                 # Mood is a vector containing a vector.

All three vectors or data structures above contain only one element. We can create longer vectors containing more elements. The useful function to assign more than one element to some name is the function: c().

Natural_Numbers <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)

The above code creates a vector containing the 1 through 9, as doubles. This vector is of length 9 (i.e., it contains 9 elements).

length(Natural_Numbers) # evaluates to 9

length(pi) # evaluates to 1

length(395.73) # evaluates to 1