---
title: "A Brief Overview of R"
output:
html_document:
toc: yes
---
# Background
* R is a language, or an environment, for data analysis and visualization.
* R is derived form the _S_ language developed at ATT Bell Laboratories.
* R was originally developed for teaching at the University of
Auckland, New Zealand, by Ross Ihaka and Robert Gentleman.
* R is now maintained by an international group of about 20
statisticians and computer scientists.
* A great strength of R is the large number of extension packages that
have been developed; the number available on CRAN recently reached
10,000.
# Basic Usage
* Interactive R uses a command line interface (CLI)
* The interface runs a read-evaluate-print loop (REPL)
* A simple interaction with the R interpreter:
```{r, prompt=TRUE, collapse=TRUE, comment=NA}
1 + 2
```
* Values can be assigned to variables using a left arrow `<-` combination:
```{r, prompt=TRUE, collapse=TRUE, comment=NA}
x <- c(1, 3, 5)
x
```
* Basic arithmetic operations work element-wise on vectors,
```{r, prompt=TRUE, collapse=TRUE, comment=NA}
x + x
2 * x
```
# A Simple Scatter Plot
```{r geyser}
with(faithful,
plot(eruptions, waiting,
xlab = "Eruption time (min)",
ylab = "Waiting time to next eruption (min)"))
```
# Fitting a Linear Regression
```{r}
fit <- with(faithful, lm(waiting ~ eruptions))
fit
```
# Adding the Regression Line to the Plot
```{r}
with(faithful,
plot(eruptions, waiting,
xlab = "Eruption time (min)",
ylab = "Waiting time to next eruption (min)"))
abline(coef(fit), col = "red", lwd = 3)
```
# Packages and Package Libraries
* Extension modules and data sets are often made available in _packages_.
* Packages are stored in folders as collections called _libraries_.
* `.libPaths()` will show you the libraries your R process will search.
* The `library` function is used to make available packages from
libraries.
* You can install packages using the `install.packages` function or
the _Install Packages_ item in the RStudio _Tools_ menu.
# A Useful Package: `ggplot2`
* The `ggplot2` package provides a powerful alternative to the base
graphics system.
* The geyser example can be done in `ggplot2` like this:
```{r}
library(ggplot2)
p <- ggplot(faithful, aes(x = eruptions, y = waiting))
p + geom_point() + geom_smooth(method = "lm", se = FALSE)
```
# Contrast to Point-and-Click Interfaces
* Even simple tasks require learning some of the R language.
* Once you can do simple tasks, you have learned some of the R language.
* More complicated tasks become easier.
* Even very complicated tasks become possible.
# R and Reproducability
* Analyses in R are carried out by running code describing the tasks
to perform.
* This code can be
* audited to make sure the analysis is right
* replayed to make sure the results are repoducable
* reused after changes in the data or on new data
* _Literate data analysis_ tools like Rmarkdown provide support for
this.
# Some R Introductions and Tutorials
* [An Introduction to R](http://cran.r-project.org/doc/manuals/R-intro.html)
gives an introduction to the language and how to use R for
statistical analysis and graphics.
* Another
[introduction to R](http://zoonek2.free.fr/UNIX/48_R/all.html) by
Vincent Zoonekynd.
* [Quick-R](http://www.statmethods.net/) web site related to *R
in Action* book.
* [R For Beginners](ftp://cran.r-project.org/pub/R/doc/contrib/Paradis-rdebuts_en.pdf).
* [TryR](http://tryr.codeschool.com/) at Codeschool.
* [swirl: Lean R in R](http://swirlstats.com/).
* There are _many_ others.
# R Markdown Tutorials
* [R Markdown: The Definitive Guide](https://bookdown.org/yihui/rmarkdown/)
by Yihui Xie is a book-length presentation.
* The [R Markdown Home Page](https://rmarkdown.rstudio.com) has a link
to a [tutorial](https://rmarkdown.rstudio.com/lesson-1.html).