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.
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.
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.
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] 3.8536614 2.7010206 1.2669345 4.1193220 0.9946142 2.8411769
head(dataset)
x
1 3.8536614
2 2.7010206
3 1.2669345
4 4.1193220
5 0.9946142
6 2.8411769
tail(x)
[1] 2.236439 1.605002 1.800112 2.991106 2.594602 1.165490
tail(dataset)
x
20 2.236439
21 1.605002
22 1.800112
23 2.991106
24 2.594602
25 1.165490
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 = 10.748, df = 24, p-value = 1.179e-10
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
1.678885 2.476875
sample estimates:
mean of x
2.07788