The R Interface

The R Interface

When you first open R, it will look like the figure below. You could start using R at this point by typing in commands at the point where the > is located. Let’s go ahead and try that.

http://i1.wp.com/freestatisticalsoftware.com/wp-content/uploads/2012/06/R-Interface.jpg?w=584

PC version of what R’s console looks like

 

Mac R console

The first thing that we’ll do is create a variable called hello and give it the value of 107. To do that, we would do the following

hello = 107
hello
## [1] 107

Notice how it returned the value “107” back to you. All you did was create something (we’ll call it a variable) called “hello” and assigned it a value of 107. Now let’s try something else. Let’s create a called weight:

 weight = c(143, 137, 137, 131, 129, 125, 125, 124, 124, 120)
 weight
##  [1] 143 137 137 131 129 125 125 124 124 120

Suppose that this represents your weekly weight over a 10 week period. (Kudos to you! You’re learning R and losing weight!). So here’s what we did–we created a variable called weight, but rather than holding a single value, it actually contains 10 values. This is called a vector. That’s why we put “c(…)” there– stands for ‘concatenate’ which is is an overly technical way to say, “I put a bunch of things into a single object.”

Since now contains several numbers, we can start doing statistics on it. For example, we may compute the mean of it.

mean(weight)
## [1] 129.5

(don’t forget to hit enter). Or we could compute the standard deviation

sd(weight)
## [1] 7.367

or the median

median(weight)
## [1] 127

Or we could plot it

plot(weight)

weightPlot

At this point, hopefully your results matched mine. If so, good job! You’ve just done your first bit of R programming.

Now go ahead and close the plot window (after spending several minutes admiring it!) Now let’s suppose that you should have put the value 135 in the third position, not 137. Oops. To correct it, there’s several things you could do. Here’s one way: you push the up arrow button six times until R shows the following:

weight = c(143, 137, 137, 131, 129, 125, 125, 124, 124, 120)

Then you could click on the third 137, change it to 135, then hit enter. Go ahead and do it. In this example, that was easy because you noticed the error only 6 steps into your code. But suppose you had done 100 things between creating the variable and noticing the error. You’d have to push the up-arrow button 100 times! So, let me show you a much better way of keeping track of your code and keeping a better record of what you’re doing.

The first thing we’ll do is hit command-N (if you’re on a PC, you’ll have to go to the file menu, then click on “New script“). Notice that a new window pops up (on the right in the image below).

R Console on a mac

R Console on a mac

The good thing about this second window is that it allows you to write code, save it, edit it, then selectively run it. Let me give you an example. On the right window (the one that you just opened), write the following code just as you did before:

 weight = c(143, 137, 137, 131, 129, 125, 125, 124, 124, 120)
 mean(weight)
 sd(weight)
 median(weight)
 plot(weight)

Then go ahead and hit command-S (or control-S) to save the document. Save it somewhere you’ll remember. I saved mine in my documents folder and called it “weightloss.” When you save a script (which is what this window is called), then you can access and run it later. Now that it’s saved, let’s go ahead and have R run the script. To do so, click the mouse anywhere on the first line and hit command-enter (on a Mac) or control-R (on a PC). Notice that on the left window it has the following:

 weight = c(143, 137, 137, 131, 129, 125, 125, 124, 124, 120)

So, what just happened? What you did was create your code in the right window, then sent that line to the left window. To run all the code, click on the right window, hit command-A (or control-A) to highlight all your code, then hit command-enter (or control-R). Notice how all of your code is run simultaneously.

The good thing about this method (i.e., the method of having two windows) is that you have one window where you can edit/write your code and the other window is dedicated to running the code. That way, if you need to make an edit, you don’t have to keep pushing the up arrow everytime you need to change something. Instead, you edit it in the right window, then execute the code (by pushing command-enter or control-R).

Next, Basic R Commands.

One thought on “The R Interface

  1. Pingback: Installing R | Dustin Fife

Leave a Reply