-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_template.Rmd
167 lines (110 loc) · 3.69 KB
/
01_template.Rmd
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
---
title: "my first notebook"
author: "‽"
date: "today's date"
output:
html_notebook:
toc: yes
toc_float: yes
---
This is an [R Notebook](https://rmarkdown.rstudio.com/r_notebooks.html) illustrating [literate programming](https://en.wikipedia.org/wiki/Literate_programming) techniques with R code **inside** *code chunks* and incorporating explanations (enhanced via [R Markdown](http://rmarkdown.rstudio.com/)) **outside** the *code chunks*. You can execute code within the R Notebook. Results appear beneath the code will also be integrated into the output documents identified in the [YAML](https://en.wikipedia.org/wiki/YAML) header .
Try executing the code chunk, below, by clicking the green-arrow *Run* button (within the chunk), or by placing your cursor inside the chunk and pressing *Ctrl+Shift+Enter.*
```{r}
plot(cars)
```
Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Ctrl+Alt+I*.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Ctrl+Shift+K* to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.
*Ctrl+Shift+M*: Generates a pipe -- ` %>% ` -- between functions (When you use the `tidyerse` package.)
###### https://rmarkdown.rstudio.com/r_notebooks.html
# Packages and the Tidyverse
###### https://tidyverse.org
```{r message=FALSE, warning=FALSE}
library(tidyverse)
```
# Hello World
This is my very first R Notebook.
My^[Your Name Here ;-j] *favorite **search** engines* are
1. [Google](http://google.com)
3. [Yahoo](http://yahoo.com)
2. [Bing](http://bing.com)
###### https://rmarkdown.rstudio.com/
# Load Data
###### https://support.rstudio.com/hc/en-us/articles/218611977-Importing-Data-with-RStudio
###### https://www.rstudio.com/resources/webinars/importing-data-into-r/
### Hurricane data from github
(Do you want to copy/paste the code? https://is.gd/rcode)
```{r message=FALSE, warning=FALSE, include=FALSE}
canes <- read_csv("https://is.gd/bigstorms")
```
```{r echo=FALSE}
canes
```
have a look at the data structures and data types
###### http://r4ds.had.co.nz/tibbles.html
```{r}
class(canes)
```
###### http://r4ds.had.co.nz/vectors.html
```{r}
glimpse(canes)
```
# dplyr
- filter (rows)
- arrange (sort rows by variables)
- select (columns)
- mutate (change cell values)
- Count
- Summarize
###### http://r4ds.had.co.nz/transform.html#introduction-2
###### http://dplyr.tidyverse.org/
```{r}
starwars
```
## Filter
```{r}
starwars_small <- starwars %>%
filter(!is.na(mass), !is.na(height))
```
## Arrange
```{r}
starwars_small %>%
arrange(desc(height), desc(name))
```
## Select
```{r}
starwars_small %>%
arrange(species, height) %>%
select(name, species, height, mass, birth_year)
```
```{r}
starwars_small %>%
select(name, species, height, mass, birth_year) %>%
arrange(species, desc(height), mass, name) %>%
filter(species == "Human")
```
## Mutate
```{r}
starwars_small %>%
select(name, species, height, mass, birth_year) %>%
arrange(species, desc(height), mass, name) %>%
filter(species == "Human") %>%
mutate(BMI = round(height / mass, 2))
```
## Count
```{r}
starwars %>%
count(mass) %>%
arrange(desc(n))
```
## Summarize
```{r}
starwars %>%
filter(!is.na(height)) %>%
group_by(species) %>%
summarise(Count = n(), mean_ht = mean(height), min_ht = min(height),
max_ht = max(height)) %>%
arrange(desc(Count))
```
# More information
https://rfun.library.duke.edu/intro2r