setting up r to use data in a beginner’s guide to r · setting up r to use data in a beginner’s...

2

Click here to load reader

Upload: nguyenkhanh

Post on 21-Jul-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Setting Up R to use Data in A Beginner’s Guide to R · Setting Up R to use Data in A Beginner’s Guide to R ... Go to the website and click on the link near the bottom of the page

Bennett, PJ PSYCH 710 Setting Up R

Setting Up R to use Data in A Beginner’s Guide to R

One of the textbooks for PSYCH 710 is A Beginner’s Guide to R (or BG2R). The book is filled withexamples of how to do simple tasks in R using data sets provided by the authors. Unfortunately, the bookdoes not do a very good job explaining how R should be set up to access these sets of data. Hence, thishandout.

The data sets are enclosed in a folder, Rbook, that can be obtained from www.highstat.com/book3.htm. Go to the website and click on the link near the bottom of the page to get the data in ascii format. Afolder entitled RBook should be downloaded onto your computer. If you are using Windows, move RBookto your C drive. If you are using OS-X, move RBook to your home directory: the actual home directory,not the Documents folder located in your home directory. You can in fact move the folder anywhere onyour computer, but the following examples will assume that you have followed my suggestions.

I assume that R is installed on your computer. If not, go to http://cran.r-project.org/. Windowsusers should click on the Windows link, then the base link, and (finally) the Download link. Apple fansshould click on the MacOS X link and then select the link for the latest version (which, right now, isR-2.11.1.pkg). Follow the instructions. Come back when you’re done. . .

Now that you’re back, please launch the R application. When R starts up it will go to a defaultworking directory which can be identified by entering the following command:

> getwd()

In the OS-X version of R, the working directory is shown in a narrow window at the top of the commandwindow. The working directory is the default location for reading and writing files. To avoid overwritingimportant files, it is a good idea to create a new working directory for each project. Suppose, for example,you were starting to analyze data from your fourth-year thesis and you wanted to keep your analyses (anddata files) separate from analyses of a project you did in your third year. First, you would exit R andcreate a folder for – let’s call it thesis – in the desired location. Next, you would return to R and setthat folder as your working directory:

> setwd('~/Documents/thesis') # for OS-X

> setwd('C:/thesis/') # for Windows

The first example, which is for OS-X, assumes that the folder thesis is located inside the Documentsfolder in your home directory. The second example, which is for Windows, assumes that the thesis folderis on your C drive. Note that there are menu-driven commands for setting the working directory: go findthem.

Now we are ready to load a data file. The data file that we will use, squid.txt, probably was createdwith a spreadsheet program like Excel and then saved as an ascii text file. The following command readsthe file and stores its contents in a variable named squid:

> squid <- read.table(file='~/RBook/squid.txt',header=TRUE) # for OS-X

> squid <- read.table(file='C:/RBook/squid.txt',header=TRUE) # for Windows

The read.table command is discussed in section 2.2.1.3 (page 48) of BG2R. Note that the ∼ symbol forthe OS-X command is shorthand for “my home directory”. So the Mac command can be read as sayingthat R should read the table stored in the file named squid.txt that resides in the folder RBook located

1

Page 2: Setting Up R to use Data in A Beginner’s Guide to R · Setting Up R to use Data in A Beginner’s Guide to R ... Go to the website and click on the link near the bottom of the page

Bennett, PJ PSYCH 710 Setting Up R

in my home directory. Setting header to TRUE simply tells R that the first line of squid.txt containsthe names of the variables. If a file does not contain variable names, then header should be set to FALSE.Note that these read.table commands will work only if the file name and directory path are specifiedcorrectly. And, no, your computer is not smart enough to correct minor errors: upper and lower caseletters are NOT the same. So, if you get an error message, make sure that the file is in the correct placeand that the file and directory names are correct.

BG2R assumes that its readers use Windows computers. Most of the examples will run in the OS-Xversion of R with only minor tweaking. This handout illustrates one of the tweaks: wherever the bookspecifies a file name as C:/RBook/filename, OS-X users will have to change it to a Mac-appropriate path(e.g., ∼/RBook/filename).

2