Skip to content

Exploring objects

Lucy M Chang edited this page Apr 4, 2018 · 51 revisions

We've talked about indexing previously. Here we'll cover other ways to explore the structure and contents of your object (particularly, data frames).

Names

Every dimension in a data structure can have assigned names. For a data frame, the columns usually have informative names (e.g., site) while the rows may have names (e.g., species) or simply numbers indicating the row number.

You can assign names from the get-go using equals signs when you create an object. It looks a little bit like an argument (and in a way it is) but you can have as many as you want.

# example in one dimension (vector)
ages <- c(Amy = 22, Brian = 29, Chris = 23)

# example in two dimensions (data frame)
people <- data.frame(first = c("Amy", "Brian", "Chris"),
                     last = c("Douglas", "Evans", "Farmer"),
                     age = c(22, 29, 23))

Exercise

Let's say I use the vector ages, which has a bunch of named elements, as the last column in my data frame. What differences do you see between this data frame and the one we defined earlier (people)?

data.frame(first = c("Amy", "Brian", "Chris"),
           last = c("Douglas", "Evans", "Farmer"),
           ages)

There are a number of functions that can be used to both (1) view the names associated with the dimensions of your data structure and (2) rename those names:

  • names()
  • rownames()
  • colnames()
  • dimnames()
  • and there are probably more...

Viewing names

By simply running the function on the object, you can see what names are already assigned to it.

rownames(iris)
names(iris)
dimnames(iris)

iris.names <- names(iris)
iris.names

Assigning names

Notice the output of the above mentioned family of functions outputs a vector of names for each dimension. We can change these names by reversing it - replacing that function's value with another vector!

# we first made the data frame hey.you on the previous page
hey.you

names(hey.you)
# should return a vector:
# [1] "X1.26"   "letters" "words" 

names(hey.you) <- c("numbers", "alphabet", "words")

hey.you
names(hey.you)

Exercise

Assign names to the three elements in the list stuff.named to "numbers", "word", and "dat" using two methods:

  1. Building it with those names (modifying the following line of code)
  2. Assigning names after you've made the list (adding a second line of code)
stuff.named <- list(c(66:101), "second", mtcars)

Displaying an object's structure

Objects, especially outputs from functions that perform a number of tasks, can be complex - they can be incredibly large and/or contain nested lists and/or consist of multiple dimensions.

(For example, objects with the class multiPhylo may contain a list of multiple phylogenetic trees, where each one of those trees is, in turn, a list containing data frames of taxon names, topology, branch lengths, etc.)

The function str() is great for exploring the structure and dimensions of your objects.

Let's go back to our list called stuff.named:

# try this:
str(stuff.named)

Exercise

The following line of code runs a linear regression of Sepal.Length and Sepal.Width in iris.

# lm stands for linear model, ~ stands for "as a function of"
lm(Sepal.Length ~ Sepal.Width, data = iris)
  1. Open the help file for lm() and take a look at what this function returns back to you (a.k.a. its value).
  2. Assign the output of the above line of code to a variable called model.iris and check out the structure of that object.

Checking the beginnings and ends of objects

head() and tail() are functions that let you view the beginning and the end of your objects. Imagine you load or create a data frame with 2000 rows, but you are unsure if a dataset loaded in properly without formatting issues or if the transformation on your data frame or on a column worked - these functions are great for making sure your data look the way you want them to look.

head(iris)

# what is the default number of rows that head() returns to you?

Exercise

  1. Return in the console the first 10 and last 10 rows of the dataset mtcars.
  2. What happens when you try to use head() and tail() on the list stuff.named?

View

The function View() is useful for eyeballing your data. This serves as an alternative to viewing your data in the console as outputs - however, it does not let you edit or move anything around.

View(iris)