Spring Deals - hours only!Up to 70% off on all courses and bundles.-Close
Creating data frames
4. Defining column vectors inside the data.frame() function


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.


Create a data frame named houses, defining its column vectors inside the data.frame() function. Here are the two column vectors you need to create:

  • ad_code: 52538, 122539
  • price_eur: 125000, 105000

Don't forget to use the = operator instead of <- when defining your vectors.