-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdplyr_librray.Rmd
173 lines (113 loc) · 3.16 KB
/
dplyr_librray.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
---
title: "dplyr_library"
author: "Zane Dax"
output:
pdf_document:
toc: yes
html_document:
toc: yes
theme: spacelab
df_print: tibble
---
# The dplyr library
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
<style type="text/css">
li {color: #196666; font-size:14;}
a {color: #cc0099; font-size:18;}
p {font-family: monaco;font-size: 18; color:#003333;}
h1 {color:#206040;}
h2 {color:#669900;}
h3 {color:#669900;}
div {color: 'red'; font-size: 18;}
</style>
Load the libraries
```{r}
library(tidyverse)
#install.packages(nycflights13)
library(nycflights13)
```
## Topics covered in this tutorial
* filter()
* arrange()
* select()
* mutate()
* groupby()
* summarize()
* left_join()
```{r}
flights = flights
airlines = airlines
```
### Filter
filter rows of our datasets based on conditions. The R pipe operator " %>% " is the *then* keyword. The "::" is the function from the library.
```{r filter example}
flights %>%
dplyr::filter(month == 1 | month == 2, day==1)
```
then we concat the pipe operator
```{r concat pipe}
flights.filtered = flights %>%
dplyr::filter(month %in% c(11, 12)) %>%
dplyr::filter(dep_time >= 700) %>%
dplyr::filter(carrier != "UA")
flights.filtered
```
### Arrange
The ``arrange()`` function is used for sorting dataframes, the ``desc()`` helper function used to specify descending rather than ascending order. SQL the sort_by.
```{r arrange function}
flights.arranged = flights.filtered %>%
arrange( year, month, day, desc(dep_delay))
flights.arranged
```
### select
The select function is used for selecting variables (columns) of the dataset. The use of '-' is to remove column, and ``rename()`` can rename column names.
```{r select function}
flights.selected = flights.arranged %>%
dplyr::select(-(hour:time_hour)) %>% # dropping hour from time_hour column
dplyr::rename("flight_time" = "air_time", "destination" = "dest")
flights.selected
```
### Mutate
The mutate function can create new variables
```{r mutate}
flights.mutated = flights.selected %>%
mutate(gain = dep_delay - arr_delay,
hours = flight_time/60,
gain_per_hour = gain/hours)
flights.mutated
```
there are mutate extensions
```{r mutate extensions}
flights.2a = flights.mutated %>%
mutate_at(.vars = c("year","month","day"), .funs = as.character)
flights.2b = flights.mutated %>%
mutate(across(.cols = c("year","month","day"), .fns = as.character ))
flights.2b
```
### Groupby, summarize
The groupby() function creates a "grouped" dataset, it is used with summarize() function.
```{r Groupby}
mean.delays = flights.mutated %>%
group_by(carrier) %>%
dplyr:: summarize(mean_delay = mean(dep_delay, na.rm= TRUE)) %>%
arrange( desc(mean_delay))
mean.delays
```
the count function is simply ``n()`` or ``tally()``
```{r tally}
carrier.counts = flights.mutated %>%
group_by(carrier) %>%
dplyr::summarise(n = n()) %>%
arrange(desc(n))
carrier.counts
```
### Joins
All the joins you could ever want
```{r joins}
airline.names = mean.delays %>%
left_join(airlines, by= c("carrier" = "carrier")) %>%
dplyr::select(name, carrier, mean_delay)
airline.names
```