Skip to content

Import Files

Cghlewis edited this page Feb 2, 2023 · 36 revisions

These examples cover basic file imports for the most classic types of files you might encounter (Excel, csv, and tab delimited), as well as an example of how to import files from a statistical package (such as SPSS).

The examples cover how to import individual files from your computer as well as all files within a directory. When importing all files within a directory you can either read each file into it's own separate data frame or bind them all together to make one large data frame.

Last, I also included examples of how to import less common file types, including grabbing files from a url, importing non-tabular data, and grabbing data from Google Sheets.

Note: There are many other packages that can be used to import data. For most of these examples I stuck with packages that fall within the tidyverse as they have always worked well for me.

Import a single file

Import multiple files and bind

Import multiple files and create separate data frames

Import sheets from one Excel file

Import Google Sheets

  • Forthcoming

Main functions used in examples

Package Functions
fs dir_ls()
haven read_sav()
readxl read_excel(); excel_sheets()
readr read_csv(); read_tsv()
purrr map_dfr(); map()

Other functions used in examples

Package Functions
curl curl_download()
base tempfile(); list.files(); list2env(); ls()
here here()
janitor compare_df_cols()
stringr str_subset(); str_remove()
here here()
utils head()
dplyr select(); pull()
purrr set_names()
tidyselect contains()

Resources

Clone this wiki locally