-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFront_MN_R_Cheatsheet.Rmd
More file actions
290 lines (225 loc) · 7.61 KB
/
Front_MN_R_Cheatsheet.Rmd
File metadata and controls
290 lines (225 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
---
title: ""
output:
html_document:
css: "css/gray_style.css"
knit: pagedown::chrome_print
---
```{r setup, include=F}
knitr::opts_chunk$set(echo=TRUE, eval=FALSE, error=F, message=F, warning=F)
library("rmarkdown")
htmltools::tagList(rmarkdown::html_dependency_font_awesome())
```
<div class="banner"><div class="left">
<h1 style="margin-left: 20px; color: #cfcfcf; margin-top: 10px; font-size: 58px;"> R <span style="font-size: 25px;"><i class="fas fa-cloud"></i><i class="fas fa-tint"></i><i class="fas fa-seedling"></i><i class="fas fa-tree"></i></span></h1>
<h2 style="margin-left: 20px; color: #cfcfcf; margin-top: -34px;"> for MINNESOTA </h2>
</div>
<div class="right" style="margin-left: 7%; width: 36%;">
<h5 style="margin-left: 20px; color: #cfcfcf; margin-top: 38px;"> Online @ <span style="font-size: 16px; color: white;"> github.com/tidy-mn/cheatsheets </span></h5>
<h5 style="margin-left: 20px; color: #cfcfcf;"> Use this reference to read data, clean column names, make plots, process dates, filter and summarize datfa, join tables, and more. </h5>
{style="float: left; width: 90px; margin-top: -100px; margin-left: -95px;"}
</div></div>
<div class="clearbox" style="clear: both;">
<div class="header"><i class="fas fa-rocket"></i> PROJECT LAUNCH </div>
<div class="bluebox" style="width: 100%; color: white;">
<div class="in-header" style="color: white; margin-top: 4px;"> New Project </div>
`File` >`New Project` > `New Directory`
<div class="in-header" style="color: white; margin-top: 4px;"> New R script</div>
`File` > `New File` > `R Script`
<div class="in-header" style="color: white; margin-top: 0;"> Where am I? </div>
```{r}
# Show current working directory
getwd()
# Set new working directory
setwd("C:/my-data-folder")
```
<div class="in-header" style="color: white; margin-top: 2px;"> Install new packages </div>
```{r}
install.packages("readr")
library(readr)
```
</div></div>
<div class="clearbox" style="width 25.4%;">
<div class="header"> STORE VALUES </div>
<div class="linebox" style="padding-top: 24px; padding-bottom: 12px;">
```{r}
# Use the left-arrow
age <- 7.2
# Text goes in quotes
porg <- "Sunshine"
# Multiple values go inside c()
droids <- c("BB8", "R2D2", "C-3PO")
# Copy an object
my_droids <- droids
# Avoid numbers, spaces, & symbols
3-droids* <- "error_invalid_name"
```
</div></div>
<div class="clearbox" style="width: 28%; margin-left: 22px;">
<div class="header"> READ DATA </div>
<div class="linebox" style="padding-bottom: 1px;">
<div class="in-header"><i class="fa fa-file-text-o"></i> Text files _(.csv, .txt, .tab)_
</div>
```{r}
library(readr)
porgs <- read_csv("txt_file.csv")
```
<div class="in-header"><i class="fa fa-file-text"></i> Excel files _(.xlsx, .xls)_
</div>
```{r}
library(readxl)
porgs <- read_excel("Excel_file.xlsx")
```
</div>
<div class="header"> CLEAN NAMES <i class="fas fa-broom"></i> </div>
<div class="linebox" style="">
```{r}
# Simplify all column names
library(janitor)
porgs <- clean_names(porgs)
# Assign new names manually
library(dplyr)
# Put new name on left: new_name = oldName
rename(porgs, mass_kg = massKG)
```
</div>
</div>
<div class="clearbox" style="width: 19%;">
<div class="header">DESCRIBE DATA</div>
<div class="linebox">
```{r}
library(dplyr)
nrow(porgs)
names(porgs)
summary(porgs)
glimpse(porgs)
class(porgs)
# View unique column values
distinct(porgs, age)
## 5 6 11 12 3
```
</div>
<div class="header"> ADD COLUMNS </div>
<div class="linebox">
```{r}
library(dplyr)
# Add home planet column
mutate(porgs,
planet = "Earth")
# Add new calculated column
mutate(porgs,
growth = height / age)
```
</div>
</div>
<div style="clear: both; margin-top: -10px;"></div>
<div class="clearbox" style="width: 48.6%; margin-top: -156px;">
<div class="header"> PLOTS </div>
<div class="linebox" style="width: 100%;">
<div style="float: left; width: 61.4%;">
<div class="in-header"> Scatterplot </div>
```{r}
library(ggplot2)
ggplot(porgs, aes(x = name, y = age)) +
geom_point(size = 8, color = "hotpink")
```
<div class="in-header"> Add titles & lines </div>
```{r}
ggplot(porgs, aes(x = name, y = age)) +
geom_point(size = 8, color = "hotpink") +
geom_hline(yintercept = 5,
linetype = "dashed") +
labs(title = "Porgs",
subtitle = "Sampled on planet Ahch-To",
caption = "data from New Republic")
```
<div class="in-header"> Facet by group </div>
```{r}
ggplot(porgs, aes(x = name, y = age)) +
geom_point(aes(color = color), size = 8) +
facet_wrap(~color) +
scale_color_manual(values = c("gray", "yellow")) +
theme_dark()
```
</div>
<div style="float: left; width: 36%; margin-left: 12px;">
<div class="linebox" style="margin-top: 16px; border: 0; background-color: white; padding-bottom: 2px; padding-left: 0;">
{style="height: 122px; width: 244px;"}
<br>
{style="margin-top: -18px; height: 160px;"}
<br>
{style="margin-top: -8px; height: 150px;"}
</div></div></div></div>
<div class="clearbox" style="margin-left: 22px; width: 28%; margin-top: -30px;">
<div class="header"> FILTER </div>
<div class="linebox" style="padding-bottom: 7px; padding-top: 8px;">
```{r}
library(dplyr)
# Keep only Porgs older than 3
filter(porgs, age > 3)
# Keep rows with name Jumpity
filter(porgs, name == "Jumpity")
# Keep Porgs named Jumpity OR Chicken
filter(porgs, name %in% c("Jumpity", "Chicken"))
```
</div>
<div class="header"> SUMMARIZE </div>
<div class="linebox" style="padding-bottom: 7px; padding-top: 8px;">
```{r}
library(dplyr)
# Summarize the age for the entire table
summarize(porgs, avg_age = mean(age))
# Summarize the age for each color group
group_by(porgs, color) %>%
summarize(avg_age = mean(age))
```
</div>
</div>
<div class="clearbox" style="width: 19%; margin-top: -30px; margin-left: 2px;">
<div class="header"> COMPARISONS </div>
<div class="fullbox">
| Symbol | Comparison |
|:-------|:-------------------------|
| `>` | greater than |
| `>=` | greater than or equal to |
| `<` | less than |
| `<=` | less than or equal to |
| `==` | equal to |
| `!=` | NOT equal to |
| `%in%` | is value X in list: `X %in% c(1,3,5)`|
| `is.na(...)` | is the value missing? |
</div>
</div>
{style="width: 70px; top: 157px; left: 1192px; float: left; position: absolute;"}
{style="width: 64px; top: 696px; left: 1192px; float: left; position: absolute;"}
```{r, echo=F, eval=F}
library(tidyverse)
# Porgs for plotting
porgs <- read_csv("https://itep-r.netlify.com/data/porg_data.csv")
p_names <- read_csv("https://itep-r.netlify.com/data/porg_names.csv")
porgs <- left_join(porgs, p_names)
porgs$color[3:4] <- "gray"
porgs$name[2] <- "Chicken"
# Scatter
ggplot(porgs, aes(x = name, y = age)) +
geom_point(color = "#008EAA", size = 8) +
theme_minimal(base_size=26)
ggsave("images/scatter_porgs_blue.png", scale = 4)
# Facet
ggplot(porgs, aes(x = name, y = age)) +
geom_point(aes(color = color), size = 8, show.legend = F) +
facet_wrap(~color, scales = "free_x", drop = T) +
scale_color_manual(values=c("#5D295F", "#FFC845")) +
theme_dark(base_size = 26)
ggsave("images/facet_porgs_dark2.png", scale = 4)
# Titles
ggplot(porgs, aes(x = name, y = age)) +
geom_point(color = "#008EAA", size = 8) +
geom_hline(yintercept = 5,
linetype = "dashed") +
labs(title = "Porgs",
subtitle = "Observed on planet Earth",
caption = "source: MN DNR") +
theme_minimal(base_size = 26)
ggsave("images/titles_porgs_blue.png", scale = 4)
```