Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Creating data frames
2. Creating data frames using vectors
Summary

Instruction

There are many different ways to create a data frame in R. Recall that a data frame is a series of column vectors arranged side by side. To create our own data frame, we first need to define the column vectors it'll contain!

Let's suppose you're working for a real estate agency and want to store the average selling prices of houses for the first three quarters of 2017. Your plan is to save those averages in a data frame named quarter_prices. Here's what you want that this data frame to look like:

period avg_price
1 Q1 2017 170195
2 Q2 2017 173937
3 Q3 2017 177938

We'll show you how to create such a data frame in a moment, but we'll first work on creating the two individual column vectors that will make up the data frame.

As you can see above, the data frame contains the columns named period (with values of "Q1 2017", "Q2 2017", and "Q3 2017") and avg_price (with values of 170195, 173937, and 177938). Since both of these vectors contain three elements, our data frame will have three rows.

Let's go ahead and create our two vectors. The vector period is already in the code editor.

Exercise

Create a second vector called avg_price. This vector contains the values 170195, 173937, and 177938.