Using R version 2 - University of Colorado Boulder



Using R version 2.14.1

R is command-line-driven statistical software that you can download for free at

. R works on both Mac and PC. We encourage you to download and install R on your personal computer so you can work on homework problems without having to be in the computer lab.

Steps for Installing R:

If you have a Mac, you should update all of your software first. (Go to apple icon in upper left, go to “Software Update” on pull-down menu).

1) Download the base distribution of R for your operating system from:

• Under the Download and Install R section, click on your operating system

o For MacOS X: Click on R-2.14.1.pkg (latest version) if you have Mac OS 10.5 or higher

o For Windows: Under Subdirectories, click on the base version and then on Download R 2.14.1 for Windows

2) Once downloaded, simply double-click to install R

• Under MacOS X, double-click the R.mpkg contained in the disk image

• Under Windows, double-click the executable R-2.14.1-win32.exe

3) Once installed, run the R program by double-clicking the R icon on your computer.

R Basics

When you start R you get the R Console window. You can type R commands at the prompt here. Commands entered will be executed from the Console Window once you hit return. Type commands in the console for temporary work (e.g., to try out a command to see if you’re doing it correctly).

However, for most real work (e.g., homework), you should work from a script. To do this, open a new window (If Mac, under File, New Document; If Windows, under File, New Script) and type commands here in your script. These commands are executed by highlighting them and then hitting (if Mac) command and return at the same time (if Windows, it’s control and return). The great thing about writing scripts is that, once you’re done, you just need to save the script and you not only have a log of everything you’ve done, you can also select all and run it to quickly re-create all the objects and statistics that you had done in your previous session.

• All commands are case sensitive, as are names of variables and datasets. For example, data2 is a different data set than Data2.

• There are various datasets that automatically come preinstalled with R. Type cars in the console window and hit enter. This dataset has two variables, “speed” and “distance” on each of 50 cars. As you see, a dataset is a matrix of data, with the columns defining the variables in the dataset and the rows defining what we call the observations.

• To get help for any command, type a question mark followed by the command at the prompt. For example: ?data.frame. The results of the search will show up in a different window.

• Anything written on a command line after a pound sign (#) is ignored by R. This is useful for putting comments into your work.

• Within one session of R, you cannot use the same data set names more than once, or the same labels for your variables more than once.

• Each variable name must be preceded by the name of the data set that it belongs to. To show the values of just one variable, you put the data set name first, followed by a dollar sign, then the variable name (with no spaces). Try this by entering cars$dist as a command.

• In addition to analyzing datasets, R functions as a calculator. Try entering the following commands:

o 3+4

o 2*18

o 5^2

o 5^2+10

o 5^(2+2)

o 10/(2+1)

• Commands can have spaces or not. So 3 + 4 is the same as 3+4.

• If you make a mistake in a command in the console window, you can retrieve old commands using the up arrow key and then edit them and resubmit them.

Entering Data

There are two primary ways of entering data, either within R itself or by importing data files from other programs (Excel).

• To enter the values of a single variable, you enter a command that gives the variable name, followed by an equal sign and then the function c (for concatentate) followed by (in parentheses) the variable’s values, separated by commas. So try the following command:

Height ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download