Creating data frames
5. Creating a data frame with read.csv()
Feature engineering
Summary

Instruction

Excellent! Creating data frames by hand can be tedious. Suppose you've got a table in Excel — are you just going to reproduce it manually in R? Of course not! You can read in data from external files. A very popular file format for distrubing data is CSV, a file containing comma-separated values.

Let's learn how to import data from a CSV file. Historical data related to housing average prices per quarters are stored in a CSV file named quarter_prices.csv. This file is stored on our computer under the directory path data.

With read.csv(), we can load data from the CSV file into the R environment and store the values in an quarter_prices data frame, like this:

quarter_prices <- read.csv("data/quarter_prices.csv")

The function read.csv() takes a single argument, which is the location of the file on your computer in string format. The data from the CSV file will be loaded into a data frame. In our case, the path to the file on our computer is data/quarter_prices.csv.

Keep in mind that Linux systems use forward slashes (/) for directory paths, but Windows systems use backslashes (\). This course runs on a Linux system, so use forward slashes in the course. On your computer use slashes depending on your operating system. Keep in mind that if you use a backslash, you have to escape it (//).

Exercise

Housing data are stored in the data/houses.csv file. Load the data from this CSV into a data frame named houses. Remember to use forward slashes when specifying the directory path.