Commonly, data will be distributed in a format that is not native to the software that you are using, such as Excel. How can you import it?
This page is specifically about importing data files from formats specific to particular foreign software. For importing standard shared formats, see Import a Delimited Data File (CSV, TSV) or Import a Fixed-Width Data File.
- Check your data after it’s imported to make sure it worked properly. Sometimes special characters will have trouble converting, or variable name formats are inconsistent, and so on. It never hurts to check!
- Import a Delimited Data File (CSV, TSV)
- Import a Fixed-Width Data File
- Export Data to a Foreign Format
Because there are so many potential foreign formats, these implementations will be more about listing the appropriate commands with example syntax than providing full working examples. Make sure that you fill in the proper filename. The filename should include a filepath, or you should Set a Working Directory.
# Load Excel files with the readxl package # install.packages('readxl') library(readxl) data <- read_excel('filename.xlsx') # Read Stata, SAS, and SPSS files with the haven package # install.packages('haven') library(haven) data <- read_stata('filename.dta') data <- read_spss('filename.sav') # read_sas also supports .sas7bcat, or read_xpt supports transport files data <- read_sas('filename.sas7bdat') # Read lots of other types with the foreign package # install.packages('foreign') library(foreign) data <- read.arff('filename.arff') data <- read.dbf('filename.dbf') data <- read.epiinfo('filename.epiinfo') data <- read.mtb('filename.mtb') data <- read.octave('filename.octave') data <- read.S('filename.S') data <- read.systat('filename.systat')
Stata can import foreign files using the File -> Import menu. Alternately, you can use the import command:
import type using filename
type can be
import can also be used to download data directly from sources like FRED).