So far, we’ve talked about creating and naming data frames, importing them, and the
tidyverse package.
Now, let’s get more close and personal with them and learn about subsetting data frames.
We’ve already talked about some of these methods when we were covering matrices & lists, so
l will try to be super quick when glossing over familiar stuff.
First, I will make use of the tidyverse and get a dataset from the dplyr package, that’s
called starwars.
It’s stored as a tibble which, as we know, is just another take on the data frame.
There are benefits to using a tibble instead of a data frame but that usually works best
for the more advanced R user.
Alright, I will save the data as a data frame, and will remove the last three columns because
all of them contain lists and that will make exploring our data difficult.
Excellent.
Now, that’s a pretty large data frame, and it isn’t very efficient to print all of
it whenever we do an operation.
From now on, whenever our data frame is hefty, I will use head() function.
It is part of a set of functions that act like a sanity check and only show you the
top six or the bottom six rows of your data.
Like this.
This is great for keeping tabs on whatever you’re doing without flooding your console
with data.
Alright!
Subsetting.
In terms of notation, all the usual suspects are here.
We can subset data frames like we would subset a matrix, or like we would index a list.
To index a specific element, we use square brackets, row index, coma, column index.
So, if I want to know where R2-D2 is from, I need to call this:
Or, I could use the column name instead of its number because that’s easier.
To ask about all the data on Princess Leia Organa, I must reference a row.
This happens by passing her row number in the square brackets and leaving a space on
the column indicator, remember?
Alternatively, if I want to see only the character names, but nothing else, I can call the first
column, leaving a space for the rows.
I will use the head() function because 87 rows is too long…
Or, I can borrow from the list subsetting notation and use double brackets to call the
name column.
Notice that in the first instance R returned a data frame, and in the second: a vector.
Why does that happen?
Well, remember how lists behaved when we were subsetting them in the previous section?
Double brackets drilled down to the individual elements and extract them without keeping
the larger structure.
The same thing happens in data frames.
Why?
Because they are two-dimensional lists.
Excellent.
Now, lists could also be subsetted using the dollar sign, and that stands true in this
situation as well!
And just like with lists, we don’t need to use quotation marks around column names.
head(my.wars$names) Again, R returns a vector.
So, be careful which notation you use!
They are not all interchangeable.
One last way to reference a column, but get a data frame in return is by using single
brackets and column name inside quotation marks.
Like this.
Okay, and how about if I want to extract multiple columns for a subset of characters?
We can use the combine function to specify what exactly we’re interested in…
And that returns a data frame, too!
Sweet!
Alright, now we know how to subset a data frame to operate on whatever part of it we
need.
That’s awesome!
But what if we got some new observations and wanted to add them to the structure?
Or if we isolated a new variable and wanted to attach it to the data frame?
You guessed it – we will cover that in the next lesson.
Thanks, everyone!
See you in the next video, and May the Force Be with You.