3 Finding the Results

Running commands in R will produce three main types of output: text or print output, data objects, and graphs.

3.1 Text Results

Examining text results is pretty obvious: output from R statements is “printed” to the Console by default.

We can use the output of a t-test as an example.

x <- rnorm(25, mean=2)
t.test(x)

You can resize your Console pane to see more (or less) of your output at once. The width of your Console affects where the lines of print output break, so adjust this before you run your commands.

The Console is a “buffer” that only holds 1000 lines of output. With a huge amount of output, you cannot scroll back to the beginning. We will later deal with this by saving output automatically.

Text output in Console

3.2 Graphs

Graphs appear in the Plots pane, in the lower right of the RStudio workspace.

Plot the data from our last exercise as an example. The following three statements produce two plots.

plot(x)
qqnorm(x)
qqline(x)

Like the Console, the Plots pane can be resized. This changes the look of the plot (the size and the aspect ratio). You can also pop a graph into a separate window with the Zoom button in the Plots toolbar.

If you have created more than one graph, you can use the left and right arrows in the Plots toolbar to scroll among your graphs.

Plots

3.3 Examining Data

Looking at data values is a little more complicated, because data comes in many forms in R. We will discuss the varieties of R data in more depth later, but for now there are three main strategies for examining data values and properties: the Environment pane, printing data to the Console, and viewing dataframes and lists.

3.3.1 Environment Pane

The Environment pane in the upper right of the RStudio workspace shows the names of any data objects currently available in your computer’s memory.

As examples, let’s save our x data as a dataframe, and also save our t.test() results as data.

x <- rnorm(25, mean=2)
dataset <- data.frame(x)
results <- t.test(x)

In the Environment pane we see that x is a numeric vector (“num”) with 25 observations (“[1:25]”), and we can see a few of the data values. For more complicated objects like dataset and results, we can click on the blue arrow next to the name to see more details of what is inside each object, including some data values. The main thing we get out of the Environment pane is metadata, information about how each data object is defined.

Looking at data objects

3.3.2 Viewing Dataframes and Lists

If we click on the name of a dataframe or list object in the Environment pane, a tab opens in the Source pane of RStudio. For a dataframe this gives us a spreadsheet-like view of the data values. For a list, it gives us a somewhat cleaner way to browse the metadata.

3.3.3 Printing Data

For basic data objects, printing them will show us the data values. To see just a few of the data values, we can use the head() or tail() functions, which give the first or last six values or rows by default.

head(x)
[1] 2.3522124 0.3677030 0.7873361 1.4047158 3.8255339 1.8758167
head(dataset)
          x
1 2.3522124
2 0.3677030
3 0.7873361
4 1.4047158
5 3.8255339
6 1.8758167
tail(x)
[1] 2.754451 1.200830 1.285062 2.724008 3.036891 1.429561
tail(dataset)
          x
20 2.754451
21 1.200830
22 1.285062
23 2.724008
24 3.036891
25 1.429561

Viewing data

How an object is printed depends on its class. The head() and tail() functions printed x and dataset differently, even though these two contain the same values. Some functions handle these different object classes differently, and these are called generic functions.

Printing a data object like results gives us formatted output, and it does not simply show the data elements it stores. Compare this output to what we saw when we viewed results by clicking on its name in the environment.

results

    One Sample t-test

data:  x
t = 11.086, df = 24, p-value = 6.325e-11
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 1.593463 2.322501
sample estimates:
mean of x 
 1.957982 

3.4 Exercises

Run the following code. What type of output does each line produce?

x <- rnorm(100)
boxplot(x)
y <- rnorm(100, mean = 1)
t.test(x, y)
plot(x, y)
x - y