-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
298 lines (298 loc) · 10.9 KB
/
.Rhistory
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
knitr::opts_chunk$set(echo = TRUE, fig.height = 4.35, fig.width = 4.75)
data(gapminder, package = "gapminder")
head(gapminder)
seq(nrow(gapminder))
head(gapminder)
obs <- nrow(gapminder)
for (i in seq(obs)) {
gapminder[["gdp"]][i] <- gapminder[i, "pop"] * gapminder[i, "gpdPercap"]
}
head(gapminder)
for (i in seq(obs)) {
gapminder[i, "gdp"] <- gapminder[i, "pop"] * gapminder[i, "gpdPercap"]
}
gapminder$gdp <- NA
gapminder$gdp <- NULL
head(gapminder)
gapminder$gdp <- NA
head(gapminder)
obs <- nrow(gapminder)
for (i in seq(obs)) {
gapminder$gdp[i] <- gapminder[i, "pop"] * gapminder[i, "gpdPercap"]
}
for (i in seq(obs)) {
gapminder[i, "gdp"] <- gapminder[i, "pop"] * gapminder[i, "gpdPercap"]
}
seq(obs)
gapminder[1, "gdp"] <- gapminder[1, "pop"] * gapminder[1, "gpdPercap"]
gapminder[1, "pop"] * gapminder[1, "gpdPercap"]
gapminder[1, "pop"]
gapminder[1, "gpdPercap"]
head(gapminder)
gapminder <- gapminder %>% select(-gpd)
library(tidyverse)
gapminder <- gapminder %>% select(-gpd)
gapminder <- gapminder %>% select(-gdp)
obs <- nrow(gapminder)
for (i in seq(obs)) {
gapminder[i, "gdp"] <- gapminder[i, "pop"] * gapminder[i, "gdpPercap"]
}
head(gapminder)
# create a vector of values that you want to repeat the function for
obs <- seq(nrow(gapminder))
obs
obs <- seq_along(nrow(gapminder))
obs
# create a vector of values that you want to repeat the function for
obs <- 1:nrow(gapminder))
# create a vector of values that you want to repeat the function for
obs <- 1:nrow(gapminder)
obs
for (i in obs) { # the function to repeat is enclosed in braces {}
gapminder[i, "log_gdpPercap"] <- log(gapminder[i, "gdpPercap"])
gapminder[i, "log_pop"] <- log(gapminder[i, "pop"])
}
head(gapminder)
le_by_year <- data.frame(year = unique(gapminder$year),
mean_le = NA)
# Create the vector to loop over
years <- unique(gapminder$year)
for (i in years) {
le_by_year[i, "mean_le"] <- mean(gapminder[gapminder$year == i, "lifeExp"],
na.rm = T)
)
}
le_by_year <- data.frame(year = unique(gapminder$year),
mean_le = NA)
# Create the vector to loop over
years <- unique(gapminder$year)
for (i in years) {
le_by_year[i, "mean_le"] <- mean(gapminder[gapminder$year == i, "lifeExp"],
na.rm = T)
}
le_by_year
le_by_year <- data.frame(year = unique(gapminder$year),
mean_le = NA)
# Create the vector to loop over
years <- unique(gapminder$year)
for (i in years) {
le_by_year <- mean(gapminder[gapminder$year == i, "lifeExp"],
na.rm = T)
}
for (i in years) {
le_by_year <- mean(gapminder[gapminder$year == i, "lifeExp"],
na.rm = T)
}
le_by_year
rm(le_by_year)
le_by_year <- c(NULL)
for (i in years) {
le_by_year$i <- mean(gapminder[gapminder$year == i, "lifeExp"],
na.rm = T)
}
years <- unique(gapminder$year)
for (i in years) {
mean_le <- mean(gapminder$lifeExp[gapminder$year == i],
na.rm = T)
paste0(i, ": ", mean_le)
}
for (i in years) {
mean_le <- mean(gapminder$lifeExp[gapminder$year == i],
na.rm = T)
paste0(i, ": ", mean_le)
}
for (i in years) {
mean_le <- mean(gapminder$lifeExp[gapminder$year == i],
na.rm = T)
cat(i, ": ", mean_le)
}
for (i in years) {
mean_le <- mean(gapminder$lifeExp[gapminder$year == i],
na.rm = T)
print(paste0(i, ": ", mean_le))
}
for (i in years) {
mean_le <- mean(gapminder[gapminder$year == i, "lifeExp"],
na.rm = T)
paste0(i, ": ", mean_le)
}
for (i in years) {
mean_le <- mean(gapminder[gapminder$year == i, "lifeExp"],
na.rm = T)
print(paste0(i, ": ", mean_le))
}
mean(gapminder[gapminder$year == 1952, "lifeExp"], na.rm = T)
gapminder[gapminder$year == 1952, "lifeExp"]
knitr::opts_chunk$set(echo = TRUE, fig.height = 4.35, fig.width = 4.75)
data(gapminder, package = "gapminder")
head(gapminder)
class(gapminder)
glimpse(gapminder)
gapminder[gapminder$year == 1952, "lifeExp"]
mean(gapminder[gapminder$year == 1952, "lifeExp"], na.rm = T)
conts <- unique(gapminder$continent)
for (i in conts) {
mean_le <- mean(gapminder[gapminder$continent == i, "lifeExp"],
na.rm = T)
print(paste0(i, ": ", mean_le))
}
gapminder$gdpPercap
log_gdpPercap <- log(gapminder$gdpPercap)
gapminder$vec_log_gdpPercap <- log(gapminder$gdpPercap)
gapminder$vec_log_gdpPercap == gapminder$log_gdpPercap
gapminder$vec_log_gdpPercap
gapminder$log_gdpPercap
log(gapminder$gdpPercap)
gapminder$vec_log_gdpPercap <- log(gapminder$gdpPercap)
gapminder$vec_log_gdpPercap == gapminder$log_gdpPercap
gapminder$vec_log_gdpPercap
gapminder$vec_log_gdpPercap == gapminder$log_gdpPercap
all(gapminder$vec_log_gdpPercap == gapminder$log_gdpPercap)
viz_lm(gapminder, "lifeExp", "gdpPercap", 1977)
viz_lm <-
function(df, dv, iv, year) {
dat <- df[df[["year"]] == year, ]
y <- log(dat[[dv]])
x <- log(dat[[iv]])
fit <- lm(y ~ x)
plot(y ~ x, main = year,
xlab = iv, ylab = dv)
lines(x, predict(fit), col = 'blue')
}
viz_lm(gapminder, "lifeExp", "gdpPercap", 1977)
here::here()
gapminder5 <- read.csv("data/gapminder5.csv", stringsAsFactors=FALSE)
head(gapminder5)
head(gapminder)
for (i in obs) { # the function to repeat is enclosed in braces {}
gapminder[i, "log_gdpPercap"] <- log(gapminder[i, "gdpPercap"])
gapminder[i, "log_pop"] <- log(gapminder[i, "pop"])
}
obs <- 1:nrow(gapminder)
for (i in obs) { # the function to repeat is enclosed in braces {}
gapminder[i, "log_gdpPercap"] <- log(gapminder[i, "gdpPercap"])
gapminder[i, "log_pop"] <- log(gapminder[i, "pop"])
}
head(gapminder)
gapminder$vec_log_gdpPercap == gapminder$log_gdpPercap
year<-2002
if(any(gapminder$year == year)){
print(paste("Record(s) for the year",year,"found."))
} else {
print(paste("No records for year",year))
}
thresholdValue <- 50
for(i in unique(gapminder$continent)) {
tmp <- mean(gapminder$lifeExp[gapminder$continent==i])
if(tmp < thresholdValue) {
print(paste("Average Life Expectancy in", i, "is less than", thresholdValue))
} else {
print(paste("Average Life Expectancy in", i, "is greater than", thresholdValue))
} # end if else condition
}
lowerThreshold <- 50
upperThreshold <- 70
for(iCountry in unique(gapminder$country)){
tmp <- mean(gapminder$lifeExp[gapminder$country==iCountry])
if(tmp < lowerThreshold){
print(paste("Average Life Expectancy in", iCountry, "is less than", lowerThreshold))
}
else if(tmp > lowerThreshold & tmp < upperThreshold){
print(paste("Average Life Expectancy in", iCountry, "is between", lowerThreshold, "and", upperThreshold))
}
else{
print(paste("Average Life Expectancy in", iCountry, "is greater than", upperThreshold))
}
}
threshold <- 60
for (i in unique(gapminder$continent)) {
tmp <- mean(gapminder$lifeExp[gapminder$continent==i])
if (tmp < threshold) {
print(paste("Mean Life Expectancy in", i, "is less than", threshold))
} else {
print(paste("Mean Life Expectancy in", i, "is greater than", threshold))
}
}
threshold <- 70
for (i in unique(gapminder$continent)) {
tmp <- mean(gapminder$lifeExp[gapminder$continent==i])
if (tmp < threshold) {
print(paste("Mean Life Expectancy in", i, "is less than", threshold))
} else {
print(paste("Mean Life Expectancy in", i, "is greater than", threshold))
}
}
lower_threshold <- 50
upper_threshold <- 70
for(i in unique(gapminder$country)){
tmp <- mean(gapminder$lifeExp[gapminder$country==i])
if(tmp < lower_threshold){
print(paste("Average Life Expectancy in", i, "is less than", lower_threshold))
}
else if(tmp > lower_threshold & tmp < upper_threshold){
print(paste("Average Life Expectancy in", i, "is between", lower_threshold, "and", upper_threshold))
}
else{
print(paste("Average Life Expectancy in", i, "is greater than", upper_threshold))
}
}
overall_mean <- mean(gapminder$pop)
for (i in unique(gapminder$country)) {
country_mean <- mean(gapminder$pop[gapminder$country==i])
if (country_mean < overall_mean) {
print(paste("Average Life Expectancy in", i, "is", country_mean))
}
}
overall_mean <- mean(gapminder$pop)
for (i in unique(gapminder$country)) {
country_mean <- mean(gapminder$pop[gapminder$country==i])
if (country_mean < overall_mean) {
mean_le <- mean(gapminder$lifeExp[gapminder$country==i])
print(paste("Average Life Expectancy in", i, "is", mean_le))
}
}
thresholdValue <- 50
for(iContinent in unique(gapminder$continent)) {
tmp <- mean(gapminder$lifeExp[gapminder$continent==iContinent])
if(tmp < thresholdValue) {
print(paste("Average Life Expectancy in", iContinent, "is less than", thresholdValue))
} else {
print(paste("Average Life Expectancy in", iContinent, "is greater than", thresholdValue))
} # end if else condition
}
lower_threshold <- 50
upper_threshold <- 70
for (i in unique(gapminder$continent)){
tmp <- mean(gapminder$lifeExp[gapminder$continent==i])
if (tmp < lower_threshold){
print(paste("Average Life Expectancy in", i, "is less than", lower_threshold))
}
else if (tmp > lower_threshold & tmp < upper_threshold){
print(paste("Average Life Expectancy in", i, "is between", lower_threshold, "and", upper_threshold))
}
else{
print(paste("Average Life Expectancy in", i, "is greater than", upper_threshold))
}
}
getwd()
gapminder <- read.csv("bootcamp-2018/data/gapminder5.csv", stringsAsFactors=FALSE)
gapminder <- read.csv(here::here("data/gapminder5.csv", stringsAsFactors=FALSE)
school <- read.csv(here::here("data/nys_schools.csv"))
census <- read.csv(here::here("data/nys_acs.csv"))
library(tidyverse)
school_dat <- school %>% left_join(nys_acs, by = c("county_name", "year"))
school_dat <- school %>% left_join(census, by = c("county_name", "year"))
school_dat <- school %>% right_join(census, by = c("county_name", "year"))
summary(lm(mean_ela_score ~ county_per_poverty))
summary(lm(mean_ela_score ~ county_per_poverty, data = school_dat))
school_dat %>% group_by(county_name, year) %>% summarize(mean_ela = mean(mean_ela_score, na.rm - T), county_per_poverty = county_per_poverty) summary(lm(mean_ela ~ county_per_poverty, data = school_dat))
school_dat %>% group_by(county_name, year) %>% summarize(mean_ela = mean(mean_ela_score, na.rm - T), county_per_poverty = county_per_poverty) summary(lm(mean_ela ~ county_per_poverty, data = .))
school_dat %>% group_by(county_name, year) %>% summarize(mean_ela = mean(mean_ela_score, na.rm - T), county_per_poverty = county_per_poverty) %>% summary(lm(mean_ela ~ county_per_poverty, data = .))
school_dat %>% group_by(county_name, year) %>% summarize(mean_ela = mean(mean_ela_score, na.rm = T), county_per_poverty = county_per_poverty) %>% summary(lm(mean_ela ~ county_per_poverty, data = .))
school_dat %>% group_by(county_name, year) %>% summarize(mean_ela = mean(mean_ela_score, na.rm = T), county_per_poverty = mean(county_per_poverty, na.rm = T)) %>% summary(lm(mean_ela ~ county_per_poverty, data = .))
school_dat %>% group_by(county_name, year) %>% summarize(mean_ela = mean(mean_ela_score, na.rm = T), county_per_poverty = mean(county_per_poverty, na.rm = T)) %>% lm(mean_ela ~ county_per_poverty, data = .)) %>% summary()
school_dat %>% group_by(county_name, year) %>% summarize(mean_ela = mean(mean_ela_score, na.rm = T), county_per_poverty = mean(county_per_poverty, na.rm = T)) %>% lm(mean_ela ~ county_per_poverty, data = .) %>% summary()
school_dat <- school_dat %>% na_if(-99)
school_dat %>% group_by(county_name, year) %>% summarize(mean_ela = mean(mean_ela_score, na.rm = T), county_per_poverty = mean(county_per_poverty, na.rm = T)) %>% lm(mean_ela ~ county_per_poverty, data = .) %>% summary()
View(school_dat)
View(school)