Hey!
Welcome back to R for Statistics and Data Science!
Before we begin, I just wanted to say thanks for sticking around.
Also, well done on keeping such a good pace!
You really are making gigantic strides and already know a ton of stuff about R!
Because this lesson is about data frames, one of R’s best structures for dealing with
sets of data, let’s recap the data structures we’ve covered so far.
You know about vectors, which is the first structure we covered.
And you also know about vectors’ two-dimensional extensions, matrices.
You are comfortable with subsetting, and doing basic operations with both.
You have learned about lists, R’s way to store tree or hierarchical data, and you know
how to access elements from a list.
Not bad at all!
And now, to the main dish… data frames!
The common limitation of vectors and matrices is that they can only store elements of a
single type.
If we try to assign objects of different kinds to either, R’s coercion takes place.
That’s inconvenient for storing any type of real data, which often include both numerical
and string variables. Using lists is equally sub-optimal.
Lists are as cumbersome as structures, as we saw in the previous section, which makes
them extremely difficult to work with directly.
Therefore, data frames are our only hope.
Think of data frames as an Excel spreadsheet: they store different types of data, simultaneously,
in a functional way.
Although the format of a data frame is a two-dimensional table, which looks a lot like a matrix, data
frames are actually lists.
They combine vectors of the same length into the columns of a table.
Data types can differ between columns, but not within columns.
That said, a row can be constructed by cells with different data types, but columns can
only contain cells of one type.
It is very unlikely that you will ever need to create a data frame manually in your day-to-day
work; data is usually imported into R from other files, like text, excel, the web, and
so on.
We will learn how to import data sets into R in the next lesson.
As for now, let’s learn how to create them ourselves.
To create a data frame, you need two things: vectors of equal length to bind into the columns
of a table, and the data.frame() function.
For my data frame I will create four vectors: one with movie titles, one with year of release,
one with length in minutes, and one with box office grossing.
As you can notice, some of these are character vectors, and others are integers.
So, let’s jam them into a data frame.
There are a few things to notice.
First, data.frame binds the vectors as columns and infers that the names of the vector correspond
to column names.
That’s pretty useful in most cases, but in case you want to rename your columns, you
can do it as we have been doing it so far.
With the names() function, like this.
Or you could assign the names directly when creating the data frame, like this.
Sweet!
Of course, you could also assign row names but with large datasets that’s not super
convenient and is generally advised against.
If you do insist on doing it, however, you can use the row.names = argument in the function.
In fact, if you want to know more about data.frame’s arguments and usage, just call help on it
and have a quick browse.
Okay, another thing to notice is that when we call the str() function on our data frame,
it looks a lot like a list structure.
And it should, because, like we said earlier, data frames are just the two-dimensional version
of lists.
Now, look closer at the MovieTitle variable.
R has stored it as a factor.
Remember when I told you factors come into play often in R?
This is one instance of it.
If you don’t want R to store your character variables as factors, you should specifically
tell it so by setting the stringsAsFactors argument to FALSE, like this.
Alright!
Awesome.
Well, that’s it for this lesson everyone!
Next, we’ll look at the Tidyverse package.
And after that, we'll go on to importing data into RStudio.
Both of those things are going to be very useful further on, because you will primarily be importing data for
most of your projects, and you'll be using the Tidyverse as you're doing your daily analyses.
See you then!
Thanks for watching and May the Code Be with You! 😊