Local Data
R packages such as readr
, arrow
, data.table
, readxl
, and haven
provide functions to read in various formats of datasets. RStudio also provides an Import Dataset wizard for common file types which can also output the requisite R code to reproduce.
Importing data
The data import features can be accessed from the Environment pane or from the Tools menu. The importers are grouped into 3 categories:
- Text data
- Excel data
- Statistical data
To access this feature, do one of the following:
- Through the RStudio menu: File > Import Dataset
- From the Environment pane use the Import Dataset dropdown menu:
Importing data from Text and CSV files
Importing “From Text (readr)” files allows reading in CSV files and in general, character delimited files using the readr package. The Import Text Data wizard provides support to:
Import from the file system or a url
Change column data types
Skip or include-only columns
Rename the data set
Skip the first N rows
Use the header row for column names
Trim spaces in names
Change the column delimiter
Encoding selection
Select quote, escape, comment and NA identifiers
For example, a csv can be imported by pasting this url https://raw.githubusercontent.com/allisonhorst/palmerpenguins/main/inst/extdata/penguins.csv and selecting “Import”.
Importing data from Text files
Importing using “From Text (base)” enables importing text files using the base R package:
Importing data from Excel files
The Excel importer provides support to:
Import from the file system or a url
Change column data types
Skip columns
Rename the data set
Select an specific Excel sheet
Skip the first N rows
Select NA identifiers
For example, the Import Excel Data wizard can import an xls file from the readxl
R package by pasting this url: https://github.com/tidyverse/readxl/blob/main/inst/extdata/deaths.xlsx?raw=true and pressing Update.
This file contains text at the top of the file and therefore, requires the first few rows to be removed, which can be accomplished by changing Skip: 4
and pressing Update.
The final step is to press Import to execute the code under Code Preview and import the data into RStudio.
Importing data from SPSS, SAS and Stata files
The SPSS, SAS and Stata importer provides support to:
Import from the file system or a url
Rename the data set
Specify a model file
An example .sav
file is available at: https://github.com/rstudio/webinars/raw/master/23-Importing-Data-into-R/data/Child_Data.sav. Paste that URL under File/Url, press Update and then press Import.