https://github.com/rrwen/r-reference
Quick reference to learning R
https://github.com/rrwen/r-reference
analysis beginner data guide introduction learn r reference statistics stats syntax
Last synced: 12 months ago
JSON representation
Quick reference to learning R
- Host: GitHub
- URL: https://github.com/rrwen/r-reference
- Owner: rrwen
- License: mit
- Created: 2016-08-11T14:54:24.000Z (almost 10 years ago)
- Default Branch: intro
- Last Pushed: 2017-08-08T17:50:49.000Z (almost 9 years ago)
- Last Synced: 2025-04-03T07:19:57.996Z (about 1 year ago)
- Topics: analysis, beginner, data, guide, introduction, learn, r, reference, statistics, stats, syntax
- Homepage:
- Size: 362 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# R Reference
Richard Wen (rrwen.dev@gmail.com)
A quick introduction to R, which includes installation, basic examples, and references to learning resources.
**Requirements**
* [R](https://www.r-project.org/) [[Download](http://cran.r-project.org/mirrors.html)]: a free programming language that is widely used for statistics and data analysis
* [RStudio](https://www.rstudio.com/products/rstudio/) [[Download](https://www.rstudio.com/products/rstudio/download2/#download)]: a graphical interface and various tools to make writing R code easier
## Contents
**[1.0 Quick Start](https://github.com/rrwen/r-reference#10-quick-start)**
**[2.0 Basic Examples](https://github.com/rrwen/r-reference#20-basic-examples)**
* [Help](https://github.com/rrwen/r-reference#help)
* [Comments and Output](https://github.com/rrwen/r-reference#comments-and-output)
* [Math](https://github.com/rrwen/r-reference#math)
* [Variables](https://github.com/rrwen/r-reference#variables)
* [Loops](https://github.com/rrwen/r-reference#loops)
* [Vectors](https://github.com/rrwen/r-reference#vectors)
* [Dataframes](https://github.com/rrwen/r-reference#dataframes)
* [Summaries and Plots](https://github.com/rrwen/r-reference#summaries-and-plots)
* [Read and Write](https://github.com/rrwen/r-reference#read-and-write)
* [Packages and Libraries](https://github.com/rrwen/r-reference#packages-and-libraries)
**[3.0 References](https://github.com/rrwen/r-reference#30-references)**
## 1.0 Quick Start
1. Install [R](http://cran.r-project.org/mirrors.html) and [RStudio](https://www.rstudio.com/products/rstudio/download2/#download)
2. Open RStudio and begin coding in R:
* **Script Editor**: Run, edit, and save R code using script files (.R)
* **Console**: Execute and output code
* **Variables/Objects**: Inspect variables and objects in the global environment
* **Help/Plots/Misc**: Visualization, help documentation, package, and file interfaces

## 2.0 Basic Examples
The following can be run in either the console or the script editor.
### Help
Get help documentation using `?`
```r
?read.table
?write.table
?vector
?data.frame
?summary
?plot
?lm
```
### Comments and Output
Commenting using `#` and console output using `print()`
```r
# This is a comment, it does not get executed
print("Some text") # It can be used on the same line as the actual code
```
### Math
Simple arithmetic operations:
* addition `+`
* subtraction `-`
* multiplication `*`
* division `/`
* exponent `^`
```r
1 + 1 # add
2 - 1 # sub
2 * 2 # multiply
10 / 2 # div
4^2 # exp
```
### Variables
Variable assignment using `<-`
```r
x <- 1
y <- x + 1 # y == 2
```
### Loops
Basic `for` loop
```r
x <- 0
for (i in 1:5) { # Add 1 to x, 5 times
x <- x + 1
}
```
### Vectors
Create a vector using `c()`
```r
height <- c(4.8, 4.5, 5.75)
eyeColor <- c("blue", "brown", "green")
eyeColor[1] # select the 1st eyeColor
height[c(1,3)] # select 1st and 3rd height
height + 0.1 # add 0.1 to all heights
```
### Dataframes
Create a dataframe (table-like structure) using `data.frame()`
```r
dataset <- data.frame(height=c(4.8, 4.5, 5.75),
eyeColor=c("blue", "brown", "green"))
dataset[1, ] # select 1st row
dataset[, 2] # select 2nd column
dataset$eyeColor # select 2nd column by name
```
### Summaries and Plots
Obtain a summary or plot using `summary()` or `plot()`
```r
summary(dataset) # mean, median, mode, stdev, etc
plot(dataset) # height vs eyeColor
```
### Read and Write
Read/write table-formatted data using `read.table`
```r
dataset <- read.table("file.txt")
write.table(dataset, "new_file.txt")
```
### Packages and Libraries
Package management using `install.packages()` and `remove.packages()`
Load package libraries using `library()`
```r
install.packages("ggplot2")
remove.packages("ggplot2")
library(utils)
```
## 3.0 References
* [Base R Cheatsheet](https://www.rstudio.com/wp-content/uploads/2016/06/r-cheat-sheet.pdf) by Mhairi McNeill for more basic R code
* [RStudio cheatsheets](https://www.rstudio.com/resources/cheatsheets/) for quick R code references
* [Online learning resources](https://www.rstudio.com/online-learning/#R) suggested by RStudio
* A short [12 page introduction to R](https://www.rstudio.com/resources/cheatsheets/) by Paul Torfs and Claudia Brauer
* Google's [R Style Guide](https://google.github.io/styleguide/Rguide.xml) for R coding practices
* [Advanced R](http://adv-r.had.co.nz/) by Hadley Wickham