-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path01b_manipulating-data-dplyr.R
170 lines (99 loc) · 3.14 KB
/
01b_manipulating-data-dplyr.R
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
# Coding Lab: Manipulating data with `dplyr`
# Ari Anisfeld
# Summer 2020
library(tidyverse)
library(readxl)
texas_housing_data <- txhousing
# selecting columns with `select()`
select(texas_housing_data, city, date, sales, listings)
select(texas_housing_data, -c(city, date, sales, listings))
select(texas_housing_data, city, date,
sales, listings, everything())
# sort rows with `arrange()`
arrange(texas_housing_data, year)
arrange(texas_housing_data, desc(year))
# the pipe operator
select(texas_housing_data, city, year, sales, volume)
# vs
texas_housing_data %>%
select(city, year, sales, volume)
#### (another example)
texas_housing_data %>%
select(city, year, month, median) %>%
arrange(desc(median))
## creating columns with `mutate()`
texas_housing_data %>%
mutate(mean_price = volume / sales) %>%
select(city, year, month, mean_price, sales, volume)
## Binary operators: Math in R
4 + 4
4 - 4
4 * 4
4 / 4
4 ^ 4
5 %% 4 # gives the remainder after dividing
## More on `mutate()`
texas_housing_data %>%
mutate(mean_price = volume / sales) %>%
select(city, year, month, mean_price, sales, volume)
texas_housing_data %>%
mutate(mean_price = volume / sales,
sqrt_mean_price = sqrt(mean_price)) %>%
select(city, year, month, mean_price, sales, volume)
## choose rows that match a condition with `filter()`
filter(texas_housing_data, year == 2013)
#### Relational operators return TRUE or FALSE
4 < 4
4 >= 4
4 == 4
4 != 4
# not true
! TRUE
# are both x & y TRUE?
TRUE & FALSE
# is either x | y TRUE?
TRUE | FALSE
! (4 > 3) # ! TRUE
(5 > 1) & (5 > 2) # TRUE & TRUE
(4 > 10) | (20 > 3) # FALSE | TRUE
## More on `filter()`
texas_housing_data %>%
filter(year == 2013,
city == "Houston")
texas_housing_data %>%
filter(year == 2013,
city == "Houston", city == "Austin") #should get an error
texas_housing_data %>%
filter(year > 2013,
city == "Houston" | city == "Austin")
texas_housing_data %>%
filter(year > 2013,
city %in% c("Houston", "Dallas", "Austin"))
## summarize data with `summarize()`
texas_housing_data %>%
filter(city %in%
c("Houston", "Dallas", "San Antonio")) %>%
summarize(median_n_sales = median(sales),
mean_n_sales = mean(sales))
texas_housing_data %>%
filter(city %in%
c("Houston", "Dallas", "San Antonio")) %>%
summarize(n_obs = n(),
n_cities = n_distinct(city))
texas_housing_data %>%
filter(city %in%
c("Houston", "Dallas", "San Antonio")) %>%
summarize(mean_price = volume / sales) #should get an error
## dplyr verbs together
texas_housing_data %>%
select(city, year, month, sales, volume) %>%
mutate(log_mean_price = log(volume / sales)) %>%
filter(year == 2013) %>%
summarize(log_mean_price_2013 = mean(log_mean_price, na.rm = TRUE))
# Won't give you the same result as
texas_housing_data %>%
select(city, year, month, sales, volume) %>%
mutate(log_mean_price = log(volume / sales)) %>%
summarize(log_mean_price = mean(log_mean_price, na.rm = TRUE)) %>%
filter(year == 2013)
# Actually this code will give you an error, try it!