-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path04-Data_wrangling.R
More file actions
164 lines (131 loc) · 3.95 KB
/
Copy path04-Data_wrangling.R
File metadata and controls
164 lines (131 loc) · 3.95 KB
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
# install readxl
#install.packages("readxl")
# load libraries
library(readxl)
library(tidyverse)
# check your working environment
getwd()
dir()
dir("data")
# load data
nfl_salary <- read_excel("data/nfl_salary.xlsx")
# inspect
summary(nfl_salary)
glimpse(nfl_salary)
View(nfl_salary)
# start with our dataframe nfl_salary and then
# group by year and then
# summarise the mean of Quarterback
nfl_salary %>%
group_by(year) %>%
summarise(qb_mean_salary = mean(Quarterback, na.rm = TRUE))
# start with our dataframe nfl_salary and then
# group by year and then
# summarise the mean of Quarterback and the mean of Cornerback
nfl_salary %>%
group_by(year) %>%
summarise(qb_mean_salary = mean(Quarterback, na.rm = TRUE),
cb_mean_salary = mean(Cornerback, na.rm = TRUE))
# for our data to be tidy we need three columns
# year, position, and salary
nfl_tidy <- nfl_salary %>%
pivot_longer(cols = -year,
names_to = "position",
values_to = "salary")
View(nfl_tidy)
# count year and position (how many observations are there?)
nfl_tidy %>%
count(year, position)
# count year and position for missing data
nfl_tidy %>%
filter(is.na(salary)) %>%
count(year, position)
# create a clean dataframe with no missing
nfl_clean <- nfl_tidy %>%
filter(!is.na(salary))
View(nfl_clean)
# start with nfl_clean and then
# group by year and position and then
# summarise with the mean of salary
nfl_clean %>%
group_by(year, position) %>%
summarise(mean_salary = mean(salary)) %>%
arrange(-mean_salary)
########### REVIEW ########### September 24 ######
# group_by()
# grouping different categories (variables)
nfl_clean %>%
group_by(position)
# summarise()
# summarize data by mapping different function to different variables
# you can use function like mean() and n() and sum() inside summarise()
nfl_clean %>%
group_by(position) %>%
summarise(mean(salary))
# filter()
# filters observations/rows in our data
nfl_clean %>%
filter(position != "Cornerback")
# arrange()
# it organizes your data by a column, alphanumeric order
# increasing order
nfl_clean %>%
arrange(position)
nfl_clean %>%
arrange(salary)
# invert the order
nfl_clean %>%
arrange(-salary)
# count()
# it counts how many of each unique value in a column/variable
nfl_clean %>%
count(position)
# pivot_longer
# it makes the data frame longer because it gathers columns names
# in a column called "name" and all the values in a second column
# called "value"
nfl_salary %>%
pivot_longer(cols = Cornerback:'Wide Receiver')
# summarise mean salary per year and position
# start with nfl_clean
# group_by year and position
# summarise mean of salary
nfl_clean %>%
group_by(year, position) %>%
summarise(mean_salary_posyear = mean(salary))
# summarise sum salary per year and position
# start with nfl_clean and then
# group_by year and position
# summarise sum of salary
nfl_clean %>%
group_by(year, position) %>%
summarise(total_salary = sum(salary),
player_count = n())
# summarise sum of salary per year and position
# create a new column with average salary
# by dividing total_salary by player_count
nfl_clean %>%
group_by(year, position) %>%
summarise(player_count = n(),
total_salary = sum(salary)) %>%
mutate(mean_salary = total_salary/player_count,
total_spent_per_year = sum(total_salary))
# add to mutate()
# percentage of total_salary by total_spent_per_year
# divide total_salary by total_spent_per_year
nfl_summary <- nfl_clean %>%
group_by(year, position) %>%
summarise(player_count = n(),
total_salary = sum(salary)) %>%
mutate(mean_salary = total_salary/player_count,
total_spent_per_year = sum(total_salary),
percent = total_salary/total_spent_per_year)
View(nfl_summary)
# x axis will be year
# y axis will be mean_salary
# color will be position
nfl_summary %>%
ggplot(aes(x = year, y = mean_salary, color = position)) +
geom_point() +
geom_line(aes(group = position)) +
theme_bw()