R Craft: Read Excel, Stata, and other Unsupported Data Files with R

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? 

R software logo

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 read_excel.

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.

library(readxl)

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')

Load haven.

library(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 read_dta.

dat <- read_dta('file_path/file_name.dta')

Read SAS files

For files ending in .sas7bdat, use the function read_sas.

dat <- read_sas('file_path/file_name.sas7bdat')

Read SPSS files

For files ending in .sav, use the function read_sav.

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!

What’s next?

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s