-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLesson 3.R
203 lines (135 loc) · 4.87 KB
/
Lesson 3.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
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
### starting to play with dplyr data
# Call libraries ----------------------------------------------------------
# install.packages("tidyverse")
# install.packages("gapminder")
library(tidyverse)
library(gapminder)
df <- gapminder
# Quick EDA ---------------------------------------------------------------
View(df)
glimpse(df)
head(df)
summary(df)
# filter ------------------------------------------------------------------
df %>%
filter(country == "Austria", year == 1962)
df %>%
filter(country == "Austria" & year == 1962)
df %>%
filter(country == "Austria" | year == 1962)
df %>%
filter(country == "Austria", year < 1962)
df %>%
filter(country == "Austria", year > 1962)
# Exercise 1 --------------------------------------------------------------
## Fill in:
## & is:
## | is:
## , is:
# filter, select ----------------------------------------------------------
df %>%
filter(country == "Austria" | year == 1962) %>%
select(country, year)
df %>%
filter(country == "Austria" | year == 1962) %>%
select(country:year)
df %>%
filter(country == "Austria" | year == 1962) %>%
select(country:year, gdpPercap)
# Exercise 2: -------------------------------------------------------------
## Fill in:
## in select using ":" does
## in select using "," does
# pull --------------------------------------------------------------------
farmer <- read_csv("farmerSales.csv")
farmer %>% select(product)
farmer %>% pull(product) %>% head(10)
# ## back to our example
# df %>%
# pull(country)
#
# ##^ Levels? what's a factor?
#
# ## KILL ALL FACTORS FOREVER!
# df %>%
# modify_if(is.factor, as.character) %>%
# pull(country)
# Exercise 3 --------------------------------------------------------------
## Please filter to country "Zimbabwe" and select the year and lifeExp
df %>%
filter(country == "Zimbabwe") %>%
select(year,lifeExp)
# Exercise 4 --------------------------------------------------------------
## produce a vector of country names with population greater than 500 000 000
## find which countries have a population of greater than 500M AND THEN
## pull a vector of the country names
df %>%
filter(pop > 500000000) %>%
pull(country) %>%
unique
##^ why factors again?
cleaned_df <- df %>%
modify_if(is.factor, as.character)
# Exercise 4a -------------------------------------------------------------
## how can we tell that we have converted the factor to the character?
glimpse(cleaned_df)
# arrange -----------------------------------------------------------------
cleaned_df %>%
filter(year == 1997) %>%
arrange(pop)
cleaned_df %>%
filter(year == 1997) %>%
arrange(desc(pop))
# QUICK EXERCISE!!! -------------------------------------------------------
## What country had the highest life expectancy in 2007?
# group_by, count, summarize ----------------------------------------------
cleaned_df %>%
filter(year == 1997) %>%
arrange(desc(pop)) %>%
count
cleaned_df %>%
filter(year == 1997) %>%
arrange(desc(pop)) %>%
group_by(country) %>%
count
cleaned_df %>%
group_by(continent, year) %>%
count
cleaned_df %>%
group_by(country) %>%
summarize(meanlife = mean(lifeExp))
# Exercise 4 --------------------------------------------------------------
## What are the 3 countries with the highest average life expectancy?
cleaned_df %>%
group_by(_______) %>%
______(meanLife = mean(lifeExp)) %>%
_________________________
## SL, Af, Ang? Or I, S, N?
# Exercise 5 --------------------------------------------------------------
## What 2 countries in Asia have the highest maximum population?
cleaned_df %>%
filter(_________ == "Asia") %>% ## deal with "in Asia" part
group_by(_________) %>% ## what grouping are we looking for in final answer
summarize(maxPop = max(_____)) %>% ## create the value that we are looking for
arrange(desc(_____)) %>% ## arrange so that the top values are at the top
head(2) %>% ## grab the first 2 rows
pull(______) ## select the portion that was asked for in the question
## Exercise 6
## What 2 continents have the highest average life expectancy?
## Start with the text description of what you will do. Then code.
## FORGET ME NOT!!!!
## Create a plot of the populations of all the countries in Oceania over time.
## Countries should be in different colors
cleaned_df %>%
______(continent == "Oceania") %>%
ggplot(aes(x == ______, y = ______, color = ________)) +
geom_point() +
geom_line()
# Exercise OH_NO!!!!!! ----------------------------------------------------
### HELP!!!!!!! I MADE A MISATAKE AND IT WONT WORK LOL. WATS WRONG LOL
cleaned_df %>%
filter(continent = "Asia") %>%
ggplot2(aes(x = year, y = gdpPercap, color = Country)) +
geom_points() %>%
geom_line()
## ^ how to improve?