4.4 R Objects

Wherever you are right now, look around your environment. Pick an object and study its attributes. It probably has a shape, a color, a weight, and many other ways of describing it. Now pick another object, and note how it is different than the first in terms of its attributes. What does the word “object” really mean? It’s often easier to give examples than to give a precise definition, but generally objects are “things you can do things with”. That is, you can usually look at them, touch them, smell them, and move them around (when appropriate/possible, of course!). Another useful definition is that objects are nouns. Different objects have different purposes and attributes. Many of these ideas will be true for R objects as well.

We’ve already introduced the concepts of objects in R in passing, but here we briefly focus on what they are and how to work with them.

Download the progress check 3 template into your scripts folder, and follow the instructions. That document should include all progress reports from Section 4.4 through (and including) Section 5.4

4.4.1 Everything is an object in R

What exactly is an object in R? As in real life, it can be difficult to give a definition, but easier to give examples. Here are some examples of objects in R:

  1. A numeric variable
  2. A vector
  3. A matrix
  4. A list
  5. A data frame
  6. A function

This list is not exhaustive, but most objects we deal with will look like one of these.

In many programming languages, functions are handled differently from other types of objects (i.e. they are not “first class” objects). In R, they are treated the same as any other type of object. You can assign them to variables, pass them to other functions, and can be returned from a function. This is similar to the behavior of Java and Python, but different from C.

4.4.2 Assigning Objects

Any object can be assigned to a variable, as we’ve been doing already. Here’s an example:

a <- "pink pineapple"

The <- is called an assignment operator. This is the most common way of assigning objects in R, but there are others. Sometimes you may see:

a = "pink pineapple"

which in most cases, has the exact same effect as using the <-, but in a few instances, it has a different effect. Our recommendation is to always use <- when making object assignments.

There are other assignment operators as well, <<-, ->>, and ->, but we will not discuss these. You can find out more with the command ?assignOps.

One neat thing you can do is assign multiple variables at the same time:

a <- b <- "Hello"
a
[1] "Hello"
b
[1] "Hello"

Even though a and b were assigned at the same time, they are still different! So if you change a with a <- “goodbye”, then the value of b will still be “Hello”.

4.4.3 Attributes

Every object in R has attributes, extra information that’s “attached” to the object. Every object has a length attribute:

a <- c(1, 2, 3, 4)
b <- c("bonjour", "au revoir")
length(a)
[1] 4
length(b)
[1] 2

Every object has a length. Try creating an example of the following and examining the length:

  1. A logical vector with 5 elements
  2. A 2 x 2 matrix
  3. The mtcars dataframe

Every R object has a mode as well, which tells you what type of object you have. Here are some examples:

mode(a)
[1] "numeric"
mode(b)
[1] "character"

Aside from these two attributes, you can list all attributes of an object like this:

attributes(mtcars)
$names
 [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
[11] "carb"

$row.names
 [1] "Mazda RX4"           "Mazda RX4 Wag"       "Datsun 710"         
 [4] "Hornet 4 Drive"      "Hornet Sportabout"   "Valiant"            
 [7] "Duster 360"          "Merc 240D"           "Merc 230"           
[10] "Merc 280"            "Merc 280C"           "Merc 450SE"         
[13] "Merc 450SL"          "Merc 450SLC"         "Cadillac Fleetwood" 
[16] "Lincoln Continental" "Chrysler Imperial"   "Fiat 128"           
[19] "Honda Civic"         "Toyota Corolla"      "Toyota Corona"      
[22] "Dodge Challenger"    "AMC Javelin"         "Camaro Z28"         
[25] "Pontiac Firebird"    "Fiat X1-9"           "Porsche 914-2"      
[28] "Lotus Europa"        "Ford Pantera L"      "Ferrari Dino"       
[31] "Maserati Bora"       "Volvo 142E"         

$class
[1] "data.frame"

To access a specific attribute of an object, you can do this:

attr(mtcars, "class")  # Get the class attribute for the mtcars data frame
[1] "data.frame"

4.4.4 Null Objects

There is a special object called the NULL object, which really just represents “nothing”. It’s used mainly if you want to remove an element from a list:

a <- list(1, 2, 3)
a[[2]] <- NULL  # Replace component 2 with "nothing"
a
[[1]]
[1] 1

[[2]]
[1] 3

Or if a function is supposed to return but doesn’t have an object to return (more on this later when we discuss functions).

4.4.5 Removing Objects

Sometimes you want to get rid of an object! In R, you can use the rm function like so:

a <- "an object"
rm(a)
a
Error in eval(expr, envir, enclos): object 'a' not found

As you can see, the error message indicates that a has been removed. Sometimes, you’d like to remove all the objects in your environment. To do this, you can use the command:

rm(list=ls())

This video discusses objects.

https://youtu.be/VrgoEoMo9ZM