Data are read into R using several functions written for this purpose. In my experience .csv format is the most well behaved. The read.csv() function is used to assign a data file to an object:
data <- read.csv("the path and file.csv",other arguments here)

Let’s take a look at the help: ?read.csv.

There is a more general function, read.table, that can read different types of files. But, it requires more programming. In addition, there is a package called foreign that has functions for reading files from other programs (e.g. read.spss reads SPSS files; read.dta reads Stata files, etc.). However, in my experience, if you are sending something from Stata to R, for example, it is better to just have Stata write a .csv file using: the outsheet function in Stata (type help outsheet in Stata for information).

Example Data File

Let’s work through an example. Start by downloading the “R_Workshop_data.csv” file from this link on my website and save the file to a known directory. Open it and take a look at what is in the file. The file contains 52 individuals and 4 variables. The variables are: respondents id (“id”), a binary variable indicating whether the respondent is male or female (“male” where “1” is male), a measure of the respondent’s age (“age”), and a measure of risky behaviors engaged in by the respondent (“risky”).

Loading Data Files

Let’s go ahead and import it into R:

First, set the directory where the file is by using the setwd("/…") function.

#read in the data file.
data <- read.csv(
    "R_Workshop_data.csv", #the data file.
    header = TRUE, # tell R to read the first row as variable names. = TRUE, # tell R to not make any conversions.
    na.strings = "." #tell R that missing values are periods.
data #look at the object.

If you go to “Open Document” in the pulldown menu, and open the file this way, you can get an idea of how R views the file and why we give it particular instructions.

Note that we could skip the step of storing the .csv file locally and simply call file from the website url. For example:

data <- read.csv(
    header=TRUE,, na.