4.1 Data in, data out

The readr package handles the reading and writing of data stored in text files.17 Here is a cheat sheet on the topic: data I/O cheat sheet. The data sets we will mainly deal with in this course are included in the aida package for convenience. Occasionally, we will also read in data stored in CSV files.

Reading a data set from a CSV file works with the read_csv function:

fresh_raw_data <- read_csv("PATH/FILENAME_RAW_DATA.csv")

Writing to a CSV file can be done with the write_csv function:

write_csv(processed_data, "PATH/FILENAME_PROCESSED_DATA.csv")

If you want to use a different delimiter (between cells) than a comma, you can use read_delim and write_delim for example, which take an additional argument delim to be set to the delimiter in question.

# reading data from a file where cells are (unconventionally) delimited by string "|"
data_from_weird_file <- read_delim("WEIRD_DATA_FILE.TXT", delim = "|")

  1. Other packages help with reading data from and writing data to other file types, such as excel sheets. Look at the data I/O cheat sheet for more information.↩︎