-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathCurtisKephartAssignment2_Sol.R
172 lines (137 loc) · 4.49 KB
/
CurtisKephartAssignment2_Sol.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
#' ---
# title: Assignment 2 example script.
# author: "Curtis Kephart"
# date: "Winter 2016"
# assignment: https://github.com/EconomiCurtis/econ294_2015/blob/master/Assignments/Econ_294_Assignment_2.pdf
# ---
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Question 0
CurtisKephartAssignment2 <- list(
firstName = "Curtis",
lastName = "Kephart",
email = "[email protected]",
studentID = 0142214
)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Question 1
# <- get(load(...)) is a handy way to load an .RData file and rename it at the same time
diamonds <- get(
load(
file = url("https://github.com/EconomiCurtis/econ294_2015/raw/master/data/diamonds.RData")
)
)
CurtisKephartAssignment2$s1a <- nrow(diamonds)
CurtisKephartAssignment2$s1b <- ncol(diamonds)
CurtisKephartAssignment2$s1c <- names(diamonds)
CurtisKephartAssignment2$s1d <- summary(diamonds$price)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Question 2
NHIS_2007_TSV <- read.table(
file = "https://github.com/EconomiCurtis/econ294_2015/raw/master/data/NHIS_2007_TSV.txt",
sep = "\t",
header = T
)
#read.delim() also works
#name doesn't matter
CurtisKephartAssignment2$s2a <- nrow(NHIS_2007_TSV)
CurtisKephartAssignment2$s2b <- ncol(NHIS_2007_TSV)
CurtisKephartAssignment2$s2c <- names(NHIS_2007_TSV)
CurtisKephartAssignment2$s2d <- mean(NHIS_2007_TSV$weight, na.rm = T)
CurtisKephartAssignment2$s2e <- median(NHIS_2007_TSV$weight)
NHIS_2007_TSV$weight <- ifelse(
NHIS_2007_TSV$weight < 800,
NHIS_2007_TSV$weight,
NA
)
CurtisKephartAssignment2$s2f <- mean(NHIS_2007_TSV$weight, na.rm = T)
CurtisKephartAssignment2$s2g <- median(NHIS_2007_TSV$weight, na.rm = T)
# for woman (SEX == 2)
CurtisKephartAssignment2$s2h <- summary(
subset(NHIS_2007_TSV,
SEX == 2)$weight)
CurtisKephartAssignment2$s2i <- summary(
subset(NHIS_2007_TSV,
SEX == 1)$weight)
# grading: either solution for s2i and s2h will be acceptable.
# in fact, SEX == 1 is for men, SEX == 2 for woman, prev solutions has these switched
# grades will be automatically adjusted.
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Question 3
vec <- c(letters,LETTERS)
CurtisKephartAssignment2$s3a <- vec[seq(2,52, by = 2)]
CurtisKephartAssignment2$s3b <- paste(vec[c(29,21,18)],collapse="")
arr <- array(
c(letters,LETTERS),
dim = c(3,3,3)
)
CurtisKephartAssignment2$s3c <- arr[,1,2]
CurtisKephartAssignment2$s3d <- arr[2,2,]
CurtisKephartAssignment2$s3e <- paste(arr[3,1,1],arr[3,1,3],arr[3,3,2], sep = "")
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Question 4
library(foreign)
org_example <- read.dta(
file = "https://github.com/EconomiCurtis/econ294_2015/raw/master/data/org_example.dta"
)
# very niave, slow
# takes about 214.55s on this system
system.time({
allYears <- sort(unique(org_example$year))
allMonths <- sort(unique(org_example$month))
allEduc <- sort(unique(org_example$educ))
rowsToMake <- length(allYears) * length(allMonths) * length(allEduc)
df.1 <- data.frame(
year = rep(NA,rowsToMake),
month = rep(NA,rowsToMake),
educ = rep(NA,rowsToMake),
rw_mean = rep(NA,rowsToMake)
)
rowCnter <- 1
for (Year in allYears){
for (Month in allMonths){
for (Educ in allEduc){
df.1$year[rowCnter] = Year
df.1$month[rowCnter] = Month
df.1$educ[rowCnter] = Educ
df.sub <- subset(
org_example,
year == Year & month == Month & educ == Educ)
rw.mean <- mean(df.sub$rw, na.rm = T)
df.1$rw_mean[rowCnter] = rw.mean
rowCnter <- rowCnter + 1
}
}
}
})
# using aggregate
# takes about 3.71s on this system
system.time({
df.2 <- aggregate(
org_example$rw,
by = list(
year = org_example$year,
month = org_example$month,
educ = org_example$educ
),
FUN = mean, na.rm = T
)
names(df.2)[4] <- "rw_mean"
})
# actually adding this to the list "CurtisKephartAssignment2"
CurtisKephartAssignment2$s4 <- df.2
# using dplyr
# takes about 0.38s on my system.
require(dplyr)
system.time({
df.3 <- org_example %>%
dplyr::group_by(year, month, educ) %>%
dplyr::summarize(
rw_mean = mean(rw, na.rm = T)
)
})
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Save solutions
save(
CurtisKephartAssignment2,
file = "Assignments/CurtisKephartAssignment2.RData"
)