This repository has been archived by the owner on Dec 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
r-intro.Rmd
148 lines (102 loc) · 2.15 KB
/
r-intro.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
---
title: "Introduction to R"
subtitle: "`http://bit.ly/cr18-r-intro`"
author: \@mauro_lepore
output: ioslides_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
collapse = TRUE
)
```
## Data Science http://r4ds.had.co.nz/
<img src="ds_pkg.png" align="right" width = 750 />
## https://www.tidyverse.org/
<img src="tidyverse.png" align="right" width = 750 />
# Transform data
## Tools for data science
```{r}
library(tidyverse)
```
## Example dataset
```{r}
library(fgeo.data)
dim(luquillo_tree6_random)
```
## Overview
```{r}
luquillo_tree6_random
tree <- luquillo_tree6_random
```
## dplyr: Main verbs
<img src="verbs.png" align="right" width = 750 />
## Filter rows with `filter()`
```{r}
filter(tree, sp == "PREMON", quadrat == "1017")
```
## Equivalent
```{r}
tree[tree$sp == "PREMON" & tree$quadrat == "1017", ]
```
## Arrange rows with `arrange()`
```{r}
arrange(tree, sp, quadrat)
```
## Arrange in descending order `desc()`
```{r}
arrange(tree, desc(sp), quadrat)
```
## Select columns with `select()`
```{r}
select(tree, sp, quadrat, treeID, status, dbh)
```
## Select range
```{r}
select(tree, treeID:quadrat)
```
## Exclude range
```{r}
select(tree, -(treeID:quadrat))
```
## Rename columns with `rename()`
```{r}
rename(tree, tree_id = treeID)
```
## Add new columns with mutate()
```{r}
mutate(tree,
dbh_mm = dbh,
dbh_m = dbh_mm / 1000
)
```
## Only new variables with `transmute()`
```{r}
transmute(tree,
dbh_mm = dbh,
dbh_m = dbh_mm / 1000
)
```
## Summarise values with `summarise()`
```{r}
by_quad_sp <- group_by(tree, quadrat, sp)
summarise(by_quad_sp,
mean_dbh = mean(dbh, na.rm = TRUE),
mean_gx = mean(gx, na.rm = TRUE),
mean_gy = mean(gy, na.rm = TRUE)
)
```
## Anything by groups with `group_by()`
```{r}
by_sp <- group_by(tree, quadrat)
summarise(by_sp,
mean_dbh = mean(dbh, na.rm = TRUE)
)
```
# Learn more
## https://www.rstudio.com/
<img src="rstudio.png" align="right" width = 750 />
## https://rstudio.cloud/learn/primers
<img src="primers.png" align="right" width = 750 />
## https://www.rstudio.com/resources/cheatsheets/
<img src="cheet.png" align="right" width = 750 />