You downloaded data files from the US government or some other organization that depends on proprietary spreadsheet software or statistical packages. How will you get those data into R?
Fortunately, other programmers have faced this issue and developed R packages that let you read data saved in various proprietary formats. Let’s explore several popular options.
Read Excel files
Microsoft’s industry-standard spreadsheet software prefers to save files in .xls or .xlsx format. Bring these spreadsheets into R using the function
You can get this function as part of the package tidyverse or from the stand-alone package readxl.
install.packages('tidyverse') # or install.packages('readxl')
Regardless of which package you install, you’ll need to load readxl separately.
You can point
read_excel to your Excel-formatted data.
dat <- read_excel('file_path/file_name.xls') # or dat <- read_excel('file_path/file_name.xlsx')
Read files from other statistical packages
Working with Stata, SAS or SPSS data? The package haven can help you read DTA, SAS7BDAT, and SAV files. Get it by installing either the package tidyverse or the package haven on its own.
install.packages('tidyverse') # or install.packages('haven')
The type of file you have will tell you which function to use next.
Read Stata files
For files ending in .dta, try the function
dat <- read_dta('file_path/file_name.dta')
Read SAS files
For files ending in .sas7bdat, use the function
dat <- read_sas('file_path/file_name.sas7bdat')
Read SPSS files
For files ending in .sav, use the function
dat <- read_sav('file_path/file_name.sav')
Read other file types
Don’t see your file type on this page?
For R’s own RDA files or for delimited file types like CSV and TSV, check our blog in two weeks to read R Craft: Read CSV, RDA, and other Supported Data Files with R.
Or open your favorite internet search engine and search r how to open [file extension] files. Read the blogs and message boards for suggestions; someone may have your solution!
With your unsupported data in R, you’re ready to replicate a study or to explore and analyze yourself! You can learn more about exploring data and getting data ready for analysis in our video R Basics: Prepare Data for Modeling and Analysis.