Importing Data

Importing data into R is fairly simple. For Stata and Systat, use the foreign package. For SPSS and SAS I would recommend the Hmisc package for ease and functionality. See the Quick-R section on packages, for information on obtaining and installing the these packages. Example of importing data are provided below.

From A Comma Delimited Text File

# first row contains variable names, comma is separator
# assign the variable id to row names
# note the / instead of \ on mswindows systems

mydata <- read.table("c:/mydata.csv", header=TRUE,
  sep=",", row.names="id")

(To practice importing a csv file, try this exercise.)

From Excel

One of the best ways to read an Excel file is to export it to a comma delimited file and import it using the method above. Alternatively you can use the xlsx package to access Excel files. The first row should contain variable/column names.

# read in the first worksheet from the workbook myexcel.xlsx
# first row contains variable names
mydata <- read.xlsx("c:/myexcel.xlsx", 1)

# read in the worksheet named mysheet
mydata <- read.xlsx("c:/myexcel.xlsx", sheetName = "mysheet")

(To practice, try this exercise on importing an Excel worksheet into R.)


# save SPSS dataset in trasport format
get file='c:\mydata.sav'.
export outfile='c:\mydata.por'.

# in R
mydata <- spss.get("c:/mydata.por", use.value.labels=TRUE)
# last option converts value labels to R factors

(To practice importing SPSS data with the foreign package, try this exercise.)

From SAS

# save SAS dataset in trasport format
libname out xport 'c:/mydata.xpt';
data out.mydata;
set sasuser.mydata;

# in R
mydata <- sasxport.get("c:/mydata.xpt")
# character variables are converted to R factors

From Stata

# input Stata file
mydata <- read.dta("c:/mydata.dta")

(To practice importing Stata data with the foreign package, try this exercise.)

From systat

# input Systat file
mydata <- read.systat("c:/mydata.dta")

Going Further

Try this interactive course: Importing Data in R (Part 1), to work with csv and xlsx files in R. To work with SAS, Stata, and other formats try Part 2.