.Rhistory 6.17 KB
Newer Older
George Mount's avatar
George Mount committed
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
library(tidyverse)
library(readxl)
region_1  <- read_excel("C:/RFiles/sales_report.xlsx", sheet = "region_1")
head(region_1)
region_2  <- read_excel("C:/RFiles/sales_report.xlsx", sheet = "region_2")
head(region_2)
region_3  <- read_excel("C:/RFiles/sales_report.xlsx", sheet = "region_3")
head(region_3)
region_1  <- read_excel("C:/RFiles/sales_report.xlsx", sheet = "region_1")
head(region_1)
region_2  <- read_excel("C:/RFiles/sales_report.xlsx", sheet = "region_2")
head(region_2)
region_3  <- read_excel("C:/RFiles/sales_report.xlsx", sheet = "region_3")
head(region_3)
region_2$category <- str_to_title(region_2$category)
head(region_2)
# Region 3 -- add the region field
# Yes, just like this!
region_3$region <- 3
head(region_3)
sales_report <- bind_rows(region_1, region_2, region_3)
dim(sales_report)
# Did we get everything?
(nrow(region_1) + nrow(region_2) + nrow(region_3)) == nrow(sales_report)
head(sales_report)
sales_report %>%
group_by(category) %>%
summarise(avg_sales = mean(sales)) %>%
arrange(category)
sales_report %>%
group_by(region) %>%
summarise(total_sales = sum(sales)) %>%
arrange(desc(total_sales))
sales_report %>%
filter(region == 1 | region == 2) %>%
arrange(id) %>%
select(-region)
sales_report %>%
filter(region == 1 | region == 2) %>%
select(-region) %>%
arrange(id)
sales_report %>%
filter(region == 1 | region == 2) %>%
select(-region) %>%
arrange(id)
sales_report %>%
filter(region == 1 | region == 2) %>%
select(-region) %>%
arrange(id)
sales_report %>%
filter(region == 1 | region == 2) %>%
arrange(id) %>%
select(-region)
library(tidyverse)
library(Lahman)
# Players table is stored as Master
data("Master")
data("HallOfFame")
lahman_inner <- inner_join(Master, HallOfFame)
dim(lahman_inner)
dim(lahman_inner)
dim(Master)
dim(HallOfFame)
# Spreadsheet viewing environment
View(lahman_inner)
# What if we just want some fields
inner_join(select(Master, nameFirst, nameLast), HallOfFame)
# What if we just want some fields
inner_join(select(Master, nameFirst, nameLast), HallOfFame)
dim(Master)
dim(HallOfFame)
# What if we just want some fields
inner_join(select(Master, nameFirst, nameLast), HallOfFame)
# Need to keep playerID in the running!
inner_join(select(Master, nameFirst, nameLast, playerID), HallOfFame)
# Compare to left join
lahman_left <- left_join(Master, HallOfFame)
dim(lahman_left)
# Compare to inner join
dim(lahman_inner)
# See the NULLs
View(lahman_left)
library(tidyverse)
library(Lahman)
# Players table is stored as Master
data("Master")
data("HallOfFame")
lahman_inner <- inner_join(Master, HallOfFame)
dim(lahman_inner)
dim(Master)
dim(HallOfFame)
# Spreadsheet viewing environment
View(lahman_inner)
# What if we just want some fields
# this will bring an error --
inner_join(select(Master, nameFirst, nameLast), HallOfFame)
# Need to keep playerID in the running!
inner_join(select(Master, nameFirst, nameLast, playerID), HallOfFame)
# Ordering doesn't matter in inner join
dim(inner_join(HallOfFame, Master))
# Compare to left join
lahman_left <- left_join(Master, HallOfFame)
dim(lahman_left)
# Compare to inner join
dim(lahman_inner)
# See the NULLs
View(lahman_left)
# What about the other way?
lahman_left_other <- left_join(HallOfFame, Master)
dim(lahman_left_other)
# What about the other way?
lahman_left_other <- left_join(HallOfFame, Master)
dim(lahman_left_other)
library(tidyverse)
library(Lahman)
data("Managers")
data("AwardsManagers")
inner_join(Managers, AwardsManagers)
ncol(Managers)
ncol(AwardsManagers)
inner_join <- inner_join(Managers, AwardsManagers)
ncol(Managers)
ncol(AwardsManagers)
dim(inner_join)
names(Managers)
names(AwardsManagers)
# Return the join of records found in both tables.
# Keep all fields except Managers$rank.
inner_join_less_m <- inner_join(select(Managers, -rank), HallOfFame)
dim(inner_join_less_m)
# Return the join of records found in both tables.
# Keep all fields except Managers$rank.
inner_join_less_m <- inner_join(select(Managers, -rank), HallOfFame)
# Return the join of records found in both tables.
# Keep all fields except Managers$rank.
inner_join_less_m <- inner_join(select(Managers, -rank), HallOfFame)
dim(inner_join_less_m)
dim(inner_join)
dim(inner_join)
dim(inner_join_less_m)
head(Managers)
dim(Managers)
dim(select(Managers, -rank))
dim(Managers)
dim(select(Managers, -rank))
dim(Managers)
# Return the join of records found in both tables.
# Keep all fields except Managers$rank.
inner_join_less_m <- inner_join(select(Managers, -rank), AwardsManagers)
# Return the join of records found in both tables.
# Keep all fields except Managers$rank.
inner_join_less_m <- inner_join(select(Managers, -rank), AwardsManagers)
dim(inner_join_less_m)
dim(inner_join)
dim(inner_join_less_m)
# Return the join of records for all found in Managers.
left_join <- left_join(Master, HallOfFame)
dim(left_join)
nrow(Managers)
# Return the join of records for all found in Managers.
left_join <- left_join(Managers, AwardsManagers)
dim(left_join)
nrow(Managers)
# How many more rows does this have than the results of 1?
nrow(left_join(Managers, AwardsManagers)) - nrow(inner_join(Managers, AwardsManagers))
# How many more rows does this have than the results of 1?
nrow(left_join(Managers, AwardsManagers)) - nrow(inner_join(Managers, AwardsManagers))
# How many more rows does this have than the results of 1?
nrow(left_join(Managers, AwardsManagers)) - nrow(inner_join(Managers, AwardsManagers))
# How many more rows does this have than the results of 1?
nrow(left_join) - nrow(inner_join)
# Return the join of records for all found in Managers.
left_join <- left_join(Managers, AwardsManagers)
dim(left_join)
View(left_join)
nrow(AwardsManagers)
nrow(Managers)
nrow(left_join)
nrow(Managers)
nrow(left_join)
library(tidyverse)
library(Lahman)
data("Teams")
teams <- Teams
teams <- filter(teams, yearID >= 2000)
teams <- group_by(teams, teamID)
summarise(teams, mean = mean(W),
min = min(W),
max = max(W))
teams %>%
filter(yearID >= 2000) %>%
group_by(teamID) %>%
summarise(mean = mean(W),
min = min(W),
max = max(W))
teams %>%
filter(yearID >= 2000) %>%
group_by(teamID) %>%
summarise(mean = mean(W),
min = min(W),
max = max(W))
winning <- teams %>%
filter(yearID >= 2000) %>%
group_by(teamID) %>%
summarise(mean = mean(W),
min = min(W),
max = max(W)) %>%
arrange(desc(mean))
head(winning)