2 Writing and Running Commands

There are two main panes in RStudio where we write and execute R commands, or R statements. We work with scripts, sequences of statements, in the script editor and we work with individual statements in the Console.

2.1 Working with Scripts

We do most of our work in RStudio by writing, running, and saving scripts. A good script documents our work and also makes it easy to go back and correct any mistakes we may later find.

If your project takes more than three or four statements to complete, you should probably be writing a script!

2.1.1 Writing Scripts

Start a new script by going to the File menu and clicking New File - R Script. We can do the same thing by clicking the New File icon on the toolbar ().

We will notice we have the usual options for opening existing files and for saving script files in the menu and on the toolbar.

We can open existing scripts by clicking on them in the Files pane, in the lower right corner of the RStudio workspace.

As an example, we will look at a one-sample t-test. We will simulate the data, so we know what the result should be, and then perform the test.

Type the following statements into a new script:

x <- rnorm(25)

This generates 25 observations from a random normal distribution with mean zero and a standard deviation of one. Then we perform a one-sample t-test, which tells us, assuming that we took a sample from a population of numbers with mean zero, the probability of observing our sample. We do not expect to reject the null hypothesis that the mean is zero.

RStudio Script Editor

2.1.2 Running Scripts

Each line in our script is a statement, a command to be run. To run these statements one at a time move your cursor anywhere in the first line and key Ctrl-Enter (or Cmd-Enter on Mac). You can also click on the Run button at the top of the script editor. Both of these actions not only run the current statement, but also move the cursor to the next statement, making it easy to walk step by step through your script.

To run more than one statement at a time, highlight all the code you want to run and then key Ctrl-Enter or click Run. (Be careful: you can highlight less than a full statement, and R will try to execute only what you highlighted!)

    One Sample t-test

data:  x
t = -1.0619, df = 24, p-value = 0.2988
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 -0.6390555  0.2048500
sample estimates:
 mean of x 

If you try this example, you will certainly get different numbers, because we are generating random data. You might even get a result where you reject the null hypothesis! Why?

Note how the first line of code, x <- rnorm(25) does not produce output in the console (instead, it creates an object, x, in our environment), while the second line, t.test(x), does produce output in the console. We will revisit the types of results that R statements create in Finding the Results.

RStudio Script Results

2.2 Working in the Console

When we are in the midst of thinking your way through a problem, we will also find it useful to issue statements in the Console. The Console is the programmer’s scratch sheet of paper.

For example, suppose we have done the first step above, generating random numbers in a vector called x. As a check, we decide we would like to see the numbers generated, and calculate their mean, which should be close to zero.

In the Console, at the > prompt, we type


and see this output:

 [1]  0.01431489 -0.41482957  0.90293815 -1.89408580 -1.10840450 -1.46390822 -1.05924538
 [8]  1.71408613  0.50669689  0.69584601  0.36261965  1.52387016 -0.27771218 -1.01708313
[15] -1.58880888  0.66428049 -1.01260901  0.41303900  0.58174488 -0.39070535 -2.19937702
[22]  0.02057405 -0.26826827 -0.49799999  0.36545896

The numbers look reasonable (most of the values are between -2 and 2, right?), so we follow up in the Console with mean(x), and we will see that the result is close to zero.

[1] -0.2171027

If we now follow up with the t.test() in our script, our result should be not statistically significant (a p-value larger than 0.05 in the second line of the output).

RStudio Console Results

2.3 Command History

R keeps track of our command history, whether the statements were run from a script or from the Console. In the Console we can scroll through this history with the up and down arrows on our keyboard.

For example, we could use feature this to quickly generate a new sample and run a new t-test by scrolling “back” (up) and executing each command in sequence.

We can also access our command history in RStudio’s History pane (upper right, tabbed with Environment). Here we can highlight one or more statements and send them to the Console. We can also send statements “to Source” (our script), turning our scratch work into something we can save.

RStudio History

2.4 Exercises

  1. Start a new script.

  2. Type the following command into your script, which generate 10 observations from a Poisson distribution where lambda equals 2: y <- rpois(10, lambda = 2).

  3. Add a line to your script that calculates the mean() of y.

  4. Run both lines of code.

  5. In your console, enter y to view its values.

  6. Save the script.