Entering Data in R

David C. Howell

There are many ways of reading data into R, and I am only going to discuss a few of them. Most texts or Web pages on R will tell you about other ways. The idea is to go to your computer or the web and open a file that you can then use.

Reading from a File on Your Computer

Suppose that you took my advice and downloaded the zipped data file for the Methods book. In my particular case the unzipped files are saved in a directory (folder) named "methods9/DataFiles". If I want to open the Add.dat file from that folder I can do it is several different ways. The first way lets me specify exactly where the file is. I just enter

theData <- read.table("Add.dat", header = TRUE )

The first line in that box saves a lot of time. It tells R that when I ask for a file, it should look in the folder named "methods9/DataFiles," on a particular thumb-drive. Remember that the "header = TRUE" is telling R that the first line of that file will contain variable names. Also remember that the slashes have to be the kind I have used here--often called "forward slashes."

An alternative way is to have R ask me to find the file. I can do this by entering

theData <- read.table(file.choose(), header = TRUE )

A dialog box will open and I can navigate my way to the appropriate file.

But hunting around on my computer takes time, and I can make things very much easier if I tell the program where to start looking. In other words, I want to give it a "default directory," and it will start looking there. One way to do this is to go to the R Console and, under file select "change directory." If you want to build that into your program rather than use the console, just enter the following command. Note that there is no "<-" or "=" sign in this command, though a reasonable person might have expected there to be one.


If you are using R Studio, go to "Session/Set Working Directory" and specify the directory that you want to use as your working directory.

Having established the working directory, the program will always start looking in that location. So now I can use the next command as an alternative.

theData <- read.table("Add.dat", header = TRUE )

and the file will automatically go to the proper directory and download the appropriate file.

Reading Data from the Web

All of the files for the Methods book are available on the Web. Therefore you don't really need to download them if you are always going to have Internet access when playing with R. All that we really have to do is to change the location where we tell R to find the data. This is actually quite simple.

WebData <- read.table("https://www.uvm.edu/~dhowell/methods9/DataFiles/
Ex10-1.dat", header = TRUE)

That's pretty easy. My eventual plan, which I may never get to, is to go back to all of the R programs that I have written for this supplement and put in both kinds of data access. In other words I will use the "URL" address and the address of a specific file on my machine. One of those will be preceded by an "#" to comment it out. If you want the specific computer address on your machine you will have to modify what I give to point to files on your own machine.

There are a few other ways that I should mention, though I rarely use them. Some people and programs like to have variables in a data file separated by a comma. This is called "csv" for "comma separated variables," and it is common to ask Excel to save files that way. If you want to enter data that way, you can enter the line shown below.

data <- read.csv("Add.dat", header = TRUE, sep = ",")

If you want to save data with a tab character separating the values, use the following. (Excel will also allow you to save that way.)

data <- read.delim("Add.dat", header = TRUE, sep = "\t")

Finally, if you happen to have data in an SPSS systems file (not a raw data file) or a SAS system file, you can download the "foreign" package, install and then load it, and type "?read.spss" or "?read.ssd" to see how to import those files. I have never done this, but you can certainly try. This brings up an important point. I bet you would have guessed that it would be read.sas, not read.ssd. Well, so did I. But when that didn't work I went to Google and typed "reading SAS files in R." Guess what--I found pages of stuff. Do not forget that Google is there!!

Now let's move on to dch: