This repository was archived by the owner on Nov 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.R
More file actions
149 lines (96 loc) · 3.28 KB
/
script.R
File metadata and controls
149 lines (96 loc) · 3.28 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
# load libraries
library(tidyverse)
gapminder <- read.csv("data/gapminder_data.csv")
head(gapminder)
# select examples
gapminder_select <- select(gapminder, country, year, lifeExp, pop)
head(gapminder_select)
select(gapminder, country, year, lifeExp, pop)
# pipes
gapminder_select <- gapminder %>% select(., country, year, lifeExp, pop)
# pipes with no placeholder
gapminder_select <- gapminder %>%
select(country, year, lifeExp, pop)
# challenge question
x <- gapminder %>%
select(continent, gdpPercap, lifeExp, year)
# filter example
gapminder_canada <- gapminder %>%
filter(country == "Canada")
# next filter example
gapminder_LE <- gapminder %>%
filter(lifeExp > 50)
head(gapminder_LE)
# another example with multiple filters
gapminder_CE <- gapminder %>%
filter(country == "Canada", lifeExp >= 80)
head(gapminder_CE)
# filter for two countries
y <- gapminder %>%
filter(country %in% c("Canada", "Mexico"))
gapminder %>%
filter(country %in% c("Cana", "Mexico"))
# Another challenger appears!!
x <- gapminder %>%
filter(continent == "Africa", year >= 1980)
# create new columns
gapminder_gdpbil <- gapminder %>%
mutate(gdp_billion = gdpPercap * pop / 10^9)
head(gapminder_gdpbil)
# chaining commands with pipes
gapminder_new <- gapminder %>%
select(country, year, pop) %>%
filter(country == "Canada")
head(gapminder_new)
# more chaining examples
gapminder_new2 <- gapminder %>%
filter(country == "Canada") %>%
select(country, continent, gdpPercap, pop) %>%
mutate(gdp_billion = gdpPercap * pop / 10^9) %>%
filter(gdp_billion >= 9)
# group_by() and summmarize()
gapminder_avgLifeExp <- gapminder %>%
group_by(country) %>%
summarise(mean_lifeExp = mean(lifeExp))
gapminder_lifeExp_stats <- gapminder %>%
group_by(country) %>%
summarise(mean_lifeExp = mean(lifeExp), sd_lifeExp = sd(lifeExp))
gapminder_lifeExp_stats <- gapminder %>%
group_by(country) %>%
summarise(num = n())
gapminder_lifeExp_stats <- gapminder %>%
group_by(country) %>%
mutate(num = n())
# using left_join()
fruits1 <- read.csv("data/fruits_table1.csv", stringsAsFactors = FALSE)
fruits2 <- read.csv("data/fruits_table2.csv", stringsAsFactors = FALSE)
fruits_joined <- left_join(fruits1, fruits2, by = "FruitID")
# final exercise
gapminder_final <- gapminder %>%
select(country, year, pop, gdpPercap) %>%
filter(year >= 1980) %>%
mutate(gdpBil = gdpPercap * pop / 10^9) %>%
group_by(country) %>%
summarise(mean_GdpBil = mean(gdpBil), sd_GdpBil = sd(gdpBil))
write_csv(gapminder_final, path = "gapminder_summary_gdpbil.csv")
# ggplot2
ggplot( data=gapminder,
aes(x=year,y=lifeExp,group=country, col=continent) ) +
geom_line() +
geom_point(col='black')
ggplot( data=gapminder,
aes(x=gdpPercap,y=lifeExp,group=country, col=continent) ) +
geom_point() +
scale_x_log10()
x <- ggplot( data=gapminder,
aes(x=gdpPercap,y=lifeExp,col=continent,group=1) )
x <- x + geom_point( alpha=0.25) +
scale_x_log10() +
geom_smooth(method="lm")
rgb(0,0,0,0.25)
gapminder_small <- gapminder %>%
filter(country %in% c("Canada", "United States", "France", "Australia"))
x <- ggplot( gapminder_small, aes(year, lifeExp, colour = continent)) +
geom_line() +
facet_wrap(~country)
ggsave(plot = x, filename = "x.png", units = "cm", width = 12, height = 10, dpi = 300)