Data Structures
A list, a vector, and a data frame walk into a bar...
Just like data types, all of our data objects in R also belong to data structures. We're already familiar with values and vectors, now read on to meet the rest of the family!
Values
The simplest data structure in R is that of a single value, like what get back from running:
frequently_used_number <- 1/40For all intents and purposes, R treats these values as vectors, just with only a single element! With that said, let's move along to some real vectors (sorry frequently_used_number).
Vectors
Vectors are the other data structure we have already ran into. They are the simplest way of storing multiple data elements in R.
Let's go ahead and make some vectors using the concatenate function c().
# a logical vector
logicals <- c(TRUE, TRUE, FALSE)
# an integer vector
integers <- c(1:10) # the colon ":" gives us the sequence
# of intergers between 1 and 10
# a numeric vector
doubles <- integers + 0.1 # here we're coercing our integer vector of
# 1 to 10 into a numeric vector of 1.1 to 10.1
# a character vector
strings <- c("a", "f", "c", "d", "e") # remember the quotation marks
# and a looooooong numeric vector, just because
long <- seq(1, 100, 0.1) # seq will generate a vector from 1 to 1000,
# in 0.1 unit stepsAt its core, R is a language built around vectors. As such, there are a lot of inbuilt functions we can use to manipulate them. We'll go over a few now.
Looking into a vector
The first thing we might want to do with our vector is look at it. Let's call our long vector and see what's in it:
Jeez thats a lot of data! Fortunately we have a couple of different ways to look at parts of the vector without looking at the whole thing.
We can look at just the first few elements with head()
or just the last few elements with tail()
or pick and choose sections of the vector by subsetting using [] brackets to select the position, or index, of the elements we want.
Note that indexing in R starts at 1. That is, if you want the first element of sample_vector you just need to type sample_vector[1]. Intuitive, right?
Modifying vectors
In addition to chopping up vectors by subsetting, we can also modify them or add to them by reassigning in the same way we'd assign a variable.
Vectors and coercion
One limitation of vectors is that they can only contain one type of data. Let's try to make a vector with some different data types.
No worries, if we do want to group together objects with different data types, we just need to use a different data structure, this brings us to lists...
Challenges
Challenge 1.1
Challenge 1.2
Lists
Lists are a lot like vectors, except you can store data of multiple types in them! To create a list, we use the list() function rather than c().
As you can see, calling a list looks quite different to calling a vector. They also show up in a new spot in the environment panel.

See that little blue circle with the white triangle on the left? Clicking on that will show us some more information about our list.

Looking into a list
Just like with vectors, we can look at the elements of a list with the head() and tail() functions and reassign them by using []brackets and indices.
However, see the double square brackets [[]] ? The output is stored in a list! If you want to access an element in a list without the list wrapper you need to use double square brackets [[]] to access objects in the list:
Finally, you can modify a list as well as add elements to it using [[]]
Modifying lists
You might be saying: "Lists are great! Why would I ever want to use vectors?". Unfortunately, when using lists there are fewer calculations and manipulations you can do to them. Fortunately, it's not at all hard to turn a list into a vector - just use the unlist() function
Challenges
Challenge 2.1
Data Frames
The last data structure we're going to look at today is the data frame. Data frames are incredibly powerful as a means for representing tabular data.
Let's say we've a collection of different observations for a group of cats.
Rather than storing them in individual lists or vectors, we can combine them all into a data frame!
Here you can see our vectors have been turned into the columns of our data frame, each named after the name of the vector that we used to create them.
Like lists, data frames appear in the data section of the environment, and clicking the blue circle will show us some more information about our data frame.

Additionally, clicking on the little table icon in the top right will open a new window next to our script showing us everything in our data frame in spreadsheet style!

Looking at and modifying data frames
Just like vectors and lists, we can access and edit parts of our data frame by using [] brackets and indices. Given that data frames are 2 dimensional, we need to specify both a row index and a column index for the entry we want to modify, separated by a comma. It looks something like this [row, column].
By leaving one of these entries blank, we can instead access the entire row [row,] or column [,column] at once.
You can also access the last few rows and the last few rows of your data frame using head(cats) and tail(cats).
We will often want to access entire columns at a time to manipulate them. In addition to accessing them via index, we can access them by name using the data_frame$column_name syntax.
Data frames as vectors and lists
You may have noticed that the operations we can do on data frames fall somewhere between what we can do with vectors, and what we can do with lists. This is no coincidence, a data frame is really just a vector/list hybrid!
In a data frame, every row is a list of attributes for an object or event (in our example, an individual cat), while every column is a vector of a specific attribute for all our data. So basically, a row is an observation and a column is a variable.
It is this vector property that makes data frames such a powerful tool for data analysis, but more on that next lesson when we jump into the tidyverse!
Challenges
R comes with some datasets. We are going to be suing the mtcars dataset for these challenges:
Challenge 3.1
Challenge 3.2
Challenge 3.3
Last updated
Was this helpful?