R can import many data formats, including .csv, .txt, .json, .xlsx, and R’s native format .Rdata. Different file types require different import commands. Data Essentials With R will mostly use .csv files imported as tibbles with the tidyverse command read_csv(). Note that read_csv() is a different command than read.csv(), a base R command that creates dataframes. In general, “.” in a function name indicates a base R command, while “_” indicates a tidyverse command.

Importing from a raw data source, such as a csv file, has a reproducibility advantage over analyzing data with an application like Microsoft Excel. Importing prevents the raw data file from being altered by the analysis and leaves a record of every step taken with the data.

7.1 CSV files

Code
tbl <- read_csv(file = "data/cholesterol.csv")

7.2 Other file types & sources

Code
# Import data from an Excel file
library(readxl)
df_excel <- read_excel('data/processed_cleveland.xlsx', 
                       sheet = "Sheet2")

# Load (stage) datasets included in base R
data(cars, USArrests, BOD)

# Load (stage) datasets included in a package
data(cancer, package = 'survival')

# Load data saved in native R format (.rdata)
load('data/ESTRADL.rdata')