Nice job! In our previous example, we created our column vectors before creating the data frame itself. However, it's possible to create vectors inside the
data.frame() function's parameter list, like so:
quarter_prices <- data.frame(
period = c( "Q1 2017", "Q2 2017", "Q3 2017"),
avg_price = c(170195, 173937, 177938))
Note that column vectors need to be of the same length. If they are not of the same length, R will throw an error. Be aware of this when creating data frames.
We moved our vector creation inside the
data.frame() function's parameter list. Instead of using the
<- assignment operator, we used the
= operator. It's important to note that if you don't use the
= operator when creating the column vectors, R will create a data frame with unusable column names.
Everything else remains the same — our newly created data frame is exactly the same as the one we created in the previous exercise.