1. Objects

flowchart LR
    A((Objects)) --> B((Functions)) --> C((Operators))
    style A fill:#1a1a1a,stroke:#1a1a1a,color:#fff
    linkStyle default stroke:#adb5bd,stroke-width:2px

R is an object oriented language. You create “objects” and can apply functions to those objects. This section covers four sub-topics — work through each tab in order.

The most basic thing you need to learn is about the assignment operator <-. This is how you create objects. The way this looks in code is the following:

object_name <- value

So lets pretend we wanted to make an object named x with a value of 4.

We would enter the following code x <- 4. Give it a try and run the following code x <- 4, then enter x and R should return 4.

Run this example

After running this, x is now an object whose value is 4.

For the R code game <- 7, what is the name of the object?

What is the value of y from the following code? x <- 3, z <- 10, y <- x + 2

Sometimes you don’t want to create an object, but will want to make a comment in your code. If you put # in front of text, R will not process it. This allows you to make comments in your code.

Your turn — create an object named uoidaho with a value of 100
Show Solution
uoidaho <- 100
Your turn — create an object called rulez with the value of 5
Show Solution
rulez <- 5
Your turn — create agreed by adding uoidaho and rulez
Show Solution
uoidaho <- 100
rulez <- 5

agreed <- uoidaho + rulez

What is the value of the object agreed from the above example?

R and computers in general are very good at following orders, the problem is that you need to be very specific or you might get an error message. R is very sensitive. When naming objects, use the following conventions:

  1. No spaces
  2. Lower case (R is sensitive to capitalization)
  3. Use _ instead of .
  4. Use descriptive names
  5. Avoid special characters like !@&
  6. Don’t start with a number (they can end with a number, but they can’t start with one)

Take the following quiz and identify the problem with the following object names.

What is wrong with the following object name? Click all that apply: University of Idaho <- 100

Your turn — fix the name University of Idaho <- 100
Show Solution
university_of_idaho <- 100

What is wrong with the following object name? 10idaho! <- 100

Your turn — fix the name 10idaho! <- 100
Show Solution
idaho <- 100

What is wrong with the following object name? university.of.idaho <- 100

Your turn — fix the name university.of.idaho <- 100
Show Solution
university_of_idaho <- 100

Objects come in many forms. In the previous section, we were making objects that represented a single atomistic value. However, objects can have complex structures such as vectors, matrix, array, dataframe, or list.

For the purpose of simplicity we will focus on vectors and dataframes. Unless you spend a lot of time programming, iterating, or looping, you will spend the majority of time working with dataframes and to a lesser extent, vectors.

A vector takes a series of atomistic values to form an object.

object_name <- vector

A useful command is the concatenate command c(). This allows you to combine different elements separated by ,.

For example, object <- c("value_1","value_2","value_3")

Your turn — create a vector named alphabet with values a, b, c, d
Show Solution
alphabet <- c("a","b","c","d")
Your turn — create a vector named pie with values 3, 14, 15, 926
Show Solution
pie <- c(3,14,15,926)

Vectors can be made with numbers, strings in "" (i.e., characters/words), objects, or a combination of the two.

Your turn — create a vector named pie_not_number with values 3, x, apple, and 7
Show Solution
pie_not_number <- c(3,"x","apple", 7)

Dataframe

Lets make a dataframe. Dataframes can be thought of as connected vectors with the same number of items.

Dataframes are composed of columns and rows and are the object structure used to analyze data. Columns represent variables, and rows represent observations in a dataframe. You can create a dataframe with the command data.frame() using the following structure:

dataframe_name <- data.frame(
  column_name_1 = vector_1,
  column_name_2 = vector_2,
  ...
)
Run this example — build a dataframe and check its class

For the value x <- 86, object x has what object structure?

For the value x <- c(86,100,2), object x has what object structure?

All objects have a class in R. This tells R how to handle the object. In dataframes, the data within a column needs a shared class, but different columns can have different classes. You can identify the class of an object with the argument/function class().

class(object_name)

For example, class(first_df) returns the value “data.frame” indicating that the object’s class is a data.frame.

What is important is that when working in a dataframe, you will want to make sure the class of each of your columns (i.e., variables) is correct. To access a column from a data frame you need to use $ in the following way:

dataframe_name$column_name

Using the $ operator, lets see what class the alpha column is from your first_df dataframe: class(first_df$alpha)

It should return “character”. This type of class is how strings are stored. The column named pie_num is a numeric class. Numbers are stored as either numeric (can include decimals) or integer (whole numbers only).

The main take away is that objects have a class and that you can determine what the class of an object is.

str() is a useful command and one that I use more than any other command in R. It shows the class, number of observations, variables, and the names and classes of each column.

Your turn — try class() and str() on first_df
Show Solution
class(first_df)
str(first_df)
Run this example — check the class of each variable using $

Referencing first_df, what class is the variable alpha?

Referencing first_df, what class is the variable vowel?