Objects, Objects, uhm Vectors

In class today, I distributed the prompt for Problem Set 1. A digital version can be accessed here: ProblemSet1.pdf. Also, for what it is worth the code we produced in class can be downloaded and reviewed here: Code-Day04.R.

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(TRUE) # evaluates to "logical"

typeof(193) # evaluates to "double"

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

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

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

Assignment

The second big concepts 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 (an object 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 object: “Hello”.

Greeting <- "Hello"

typeof(Greeting) # will evaluate to "character"

Vectors

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

Another useful function to create vectors is: seq(). The seq() function accepts three arguments:

  1. from
  2. to
  3. by or length.out

It is used to create sequences from some starting point to some end point in increments one can specify with by.

x <- seq(from = 0, to = 3, by = 0.5) # this creates a vector
                                     # it is of length 7
                                     # and contains the values: 0.0,
                                     # 0.5, 1.0, 1.5, 2.0, 2.5, and
                                     # 3.0

Alternatively you can create a sequence of a pre-specified length – say 5 – and let R figure out the correct increments.

y <- seq(from = 0, to = 3, length.out = 5) # this creates a vector
                                           # it is of length 5
                                           # and contains the values:
                                           # 0.00, 0.75, 1.50, 2.25, 
                                           # and 3.00