Data Frames in R – Extending a data frame
Articles Blog

Data Frames in R – Extending a data frame

November 12, 2019

Hey, class, good to see you! Let’s pick it up where we left off last
time. So, we talked about subsetting a data frame,
and concluded it is only natural we also know how to extend a data frame. And that is absolutely the case! What if you need to add a couple of observations
to your data, or a new variable? Well, adding a new variable is easier than adding an observation, so let’s start there. Remember that data frames are lists? Adding a column happens just like you would
expect for lists. Let’s use the Star Wars box office data
frame we created in the beginning. I would like to add two variables called MarkScreenTime
& CarrieScreenTime to see how much screen time Mark Hamill and Carrie Fisher got per
film (these are the guys who play Luke Skywalker and Princess Leia in the movies). I already have the vectors ready… so I’m just going to paste them in… All we need to do now is choose a method for
attaching these columns. We can do it with a dollar sign, like this. Or with double brackets. (I will just remove the columns really quickly,
so we ca re-add them with the new method now…) This is the easiest way to remove variables
from a data frame, by the way. Just index the variables you want removed
and set it to NULL. Ok, we are ready to re-add Mark- and CarrieScreenTime,
but this time with double brackets. Alright, awesome! Finally, you can also use the cbind() function
we used to expand matrices. It will work just as well, with the added
benefit that you will be able to quickly add more than one variable to your data frame. What did I tell you? Expanding the variables of a data frame is
easy! That said, let’s learn how to add observations,
too 😉 Can you guess where the first tripping stone
in adding a row to a data frame comes from? I will give you a hint: all we needed to do
to extend our variables was to create a vector of values and attach it. Do you think that’s going to work for rows,
too? Nope, because vectors can only hold values
of a single basic type. Okay! What we need to do is create a small one-row
data frame. Let’s say I want to add some data about
Rogue One: A Star Wars story. Uh-oh! I have the figures I’ve created my data frame, but R is still not letting me bind it. It’s letting me know “names do not match
previous names”. Right! I should have labelled the variables in my
new data frame accordingly. Let’s mend this and give it another try. There! That worked! Fantastic! To recap, we can add columns to a data frame
in three ways, each simpler than the other… we can use list notations and go for the dollar
sign, or the double brackets. Or we can cbind() a column to the structure. Rows are more temperamental and we are expected
to use rbind() to actually attach a small data frame to the bottom of the table. We also need to name our variables with their
corresponding names in the table we want to attach them to. Great. That’s it for this lesson, guys! Play around with some data frames before you
get to the actual exercises: R has a decent collection or preloaded data frames. Just type data() and it’ll show you what
it has in store. See you next time! And May the Code Be with You ūüôā

Only registered users can comment.

Leave a Reply

Your email address will not be published. Required fields are marked *