-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz-week4.R
48 lines (41 loc) · 1.6 KB
/
quiz-week4.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
library(readr)
q1 <- function () {
sourceURL <- URLdecode('https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Fss06hid.csv')
d <- read_csv(sourceURL)
n <- strsplit(names(d), "wgtp")
print(n[123])
}
q2 <- function () {
sourceURL <- URLdecode('https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2FGDP.csv')
d <- read_csv(sourceURL)
d <- d[5:194, c(1,3,4,5)]
colnames(d) <- c('countrycode', 'ranking', 'countryname', 'gdp')
gdpNumeric <- as.numeric(gsub(',', '', d$gdp))
print(mean(gdpNumeric))
}
q3 <- function () {
sourceURL <- URLdecode('https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2FGDP.csv')
d <- read_csv(sourceURL)
d <- d[5:194, c(1,3,4,5)]
colnames(d) <- c('countrycode', 'ranking', 'countryname', 'gdp')
length(grep("^United", d$countryname, value=TRUE))
}
q4 <- function () {
gdpURL <- URLdecode('https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2FGDP.csv')
g <- read_csv(gdpURL)
g <- g[5:194, c(1,3,4,5)]
colnames(g) <- c('countrycode', 'ranking', 'countryname', 'gdp')
eduURL <- URLdecode('https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2FEDSTATS_Country.csv')
e <- read_csv(eduURL)
m <- merge(g, e, by.x='countrycode', by.y='CountryCode')
mYE <- m[grep('^Fiscal year end: June', m[['Special Notes']]),]
print(nrow(mYE))
}
q5 <- function () {
library(quantmod)
amzn <- getSymbols("AMZN",auto.assign=FALSE)
sampleTimes <- index(amzn)
samples2012 <- grep('^2012-', sampleTimes, value=TRUE)
print(length(samples2012))
print(sum(weekdays(as.Date(samples2012)) == 'Monday'))
}