This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com. I will be using R Markdown to develop R demos in class, and will share R Markdown code so that you can become familiar with it as well.
There are a couple versions of R available, though I find students prefer RStudio because of its attractive IDE (Interactive Development Environment). After installing the appropriate version of R from its website (https://cran.r-project.org), then optionally installing RStudio (https://www.rstudio.com), you can open R to begin; we will assume you have installed R Studio. When you first open R Studio, your screen will typically be divided into 4 frames.
The upper left-hand frame is used for code development, the lower left-hand frame is the console–as though R commands were being entered manually, the upper right-hand frame includes information about active R variables and data objects, while the lower right-hand screen includes plots, help and R packages.
In this first R session, we will assume that we have saved a tab-delimited two sample data in the file Examp9p4.txt and placed it in a STAT 540 folder. The file includes a grouping variable (named Group, appropriately enough) with levels New and Standard and a response variable named Value. To simplify reading the data set into R, select Session, then Set Working Directory->Choose Directory, browse to STAT 540 and select Open. R will now assume this is the location to read input files and place output files.
The commands below read the tab-delimited table, store it in a data frame, which is R’s main data object, print the data frame (Example_1), and then print summary information about the variables in the data frame.
Example_1=read.table("Examp9p4.txt",header=T)
Example_1
## Group Value
## 1 New 80
## 2 Standard 79
## 3 New 76
## 4 Standard 73
## 5 New 70
## 6 Standard 72
## 7 New 80
## 8 Standard 62
## 9 New 66
## 10 Standard 76
## 11 New 85
## 12 Standard 68
## 13 New 79
## 14 Standard 70
## 15 New 71
## 16 Standard 86
## 17 Standard 75
## 18 New 81
## 19 Standard 68
## 20 New 76
## 21 Standard 73
## 22 Standard 66
summary(Example_1)
## Group Value
## New :10 Min. :62.00
## Standard:12 1st Qu.:70.00
## Median :74.00
## Mean :74.18
## 3rd Qu.:79.00
## Max. :86.00
Note the changes in the other frames; the environment frame now contains information about Example_1, and the console includes the commands and command output.
The next set of commands can print histograms side by side; they require loading an R package, lattice, to do so; lattice has a simpler syntax for side-by-side histograms that R’s default hist() command lacks; look for the graph in the lower right-hand frame under the Plots tab. If you want to repeat a command (or re-execute it after a slight edit), go to the console and hit the “up” arrow on your keypad, and R will retrieve the command. Now hit the return key and the command will be executed again.
library(lattice)
histogram(~ Value | Group, data=Example_1)
Side-by-side boxplots do not require the lattice package (though since lattice has been loaded, its data sets and functions are available for the remainder of this R session). Note that the boxplot command supports an equation syntax that is not available in the default hist() command.
boxplot(Value~Group, data=Example_1)