9.2 Directly Reading CSV Files

You can create comma-separated-value (CSV) data files from Excel. For a particular worksheet, you select File > Save As and after you select the folder as to where to save the data, you select the CSV option for the Save File Type that is under your choice of file name. Note that the file extension of these data will be .csv, not .xlsx.

A direct way of importing your data that are in a CSV format is with the following command:

dat <- read.csv("your.path/filename.csv", header=TRUE)

Here you would substitute the location of your file (i.e. the file directory where it is stored), as well as the particular file name. Note that to reference a file in the path, you use either one forward slash (/) as above, or two backward slashes (\\).

For those working on a Mac, you can start your file path with a tilde (\(\sim\)) that takes the place of your basic user information. For example, you can enter one of the two statements to load MYMEPS.csv in R (if the MYMEPS.csv file is in your Downloads folder):

MEPS <-read.csv("~/Downloads/MyMEPS.csv", header=TRUE)  
MEPS <- read.csv("C://Macintosh/Users/YourName/Downloads/MyMEPS.csv", 
        header=TRUE)  

The <- is called an assignment operator. The information on the right-hand side is assigned to the object with the name on the left-hand side. Be sure that there is NO space between the < and the - .

In this example, MEPS, the left-hand-side of the <-, is what is called a data frame. More specifics about what is a data frame and how to manipulate it, is discussed in Section 10.