Importing data

Steen Flammild Harsted & Søren O´Neill

The Workflow

The Workflow

Importing Data

Using Rstudio

Import functions

Be reproducible and script your data import:

haven::read_dta()    # Read Stata .dta files
readr::read_csv()    # Read comma separated files
readr::read_delim()  # Read files separated (delimited) by other things (;, :, or " ")
vroom::vroom()       # Fast version of the above two, with some additional features
readxl::read_excel() # Read excel files


Use the functions with here() ::: {.cell}

read_csv(file = here("raw_data", "soldiers.csv"))

:::




read.csv() vs. read_csv()


read_csv() vs. read_csv2()

Selected file handling functions

list.files()                   # Gives a vector of filenames
list.files(pattern = "csv")    # Gives a vector of filenames containing 'csv'
file.exists()   # TRUE/FALSE if a given file exists
file.choose()   # Opens an interactive box that lets you choose a file via browsing

Example with multiple excel files

filenames <- list.files(here("raw_data", pattern = ".xls"))
dataframe <- readxl::read_excel(here("raw_data", filenames))