homework2stat3080
pdf
keyboard_arrow_up
School
University of Virginia *
*We aren’t endorsed by this school
Course
3080
Subject
Astronomy
Date
Dec 6, 2023
Type
Pages
8
Uploaded by MinisterRam1506
Homework 2
Rohan Palle
Problem 1
(a)
# Create the matrix with commute times
commutes <- matrix(c(
29, 27,
38, 32,
31, 44,
34, 37,
36, 38
), nrow = 5, byrow = TRUE)
commutes
##
[,1] [,2]
## [1,]
29
27
## [2,]
38
32
## [3,]
31
44
## [4,]
34
37
## [5,]
36
38
(b)
rownames(commutes) <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
colnames(commutes) <- c("Week1","Week2")
commutes
##
Week1 Week2
## Monday
29
27
## Tuesday
38
32
## Wednesday
31
44
## Thursday
34
37
## Friday
36
38
1
(c)
commuteFast <- commutes[,1 ] < commutes[,2 ]
commuteFast
##
Monday
Tuesday Wednesday
Thursday
Friday
##
FALSE
FALSE
TRUE
TRUE
TRUE
(d)
avgComm <- apply(commutes, 1, mean)
avgComm
##
Monday
Tuesday Wednesday
Thursday
Friday
##
28.0
35.0
37.5
35.5
37.0
(e)
budgetTime <- 35
diff <- commutes - budgetTime
diff
##
Week1 Week2
## Monday
-6
-8
## Tuesday
3
-3
## Wednesday
-4
9
## Thursday
-1
2
## Friday
1
3
(f)
avgDiff <- apply(diff, 2, mean)
avgDiff
## Week1 Week2
##
-1.4
0.6
2
(g)
maxDelay <- apply(diff, 2, max)
maxDelay
## Week1 Week2
##
3
9
(h)
within30 <- rownames(commutes)[which(commutes[,2] <= 30)]
within30
## [1] "Monday"
(i)
budgFunc <- commutes <= budgetTime
daysBudget <- apply(budgFunc, 2, sum)
daysBudget
## Week1 Week2
##
3
2
(j)
fastestWk1 <- rownames(commutes)[which(commutes[,1 ] == min(commutes[,1]))]
fastestWk1
## [1] "Monday"
(k)
similar_commutes <- commutes[which(diff[,1] * diff[,2] >= 0),]
similar_commutes
##
Week1 Week2
## Monday
29
27
## Friday
36
38
3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Problem 2
library("car")
## Loading required package: carData
data(Davis)
(a)
weight.metric <- data.frame(Davis[, c("weight", "repwt")])
head(weight.metric)
##
weight repwt
## 1
77
77
## 2
58
51
## 3
53
54
## 4
68
70
## 5
59
59
## 6
76
76
(b)
weight.imp <- weight.metric * 2.2
head(weight.imp)
##
weight repwt
## 1
169.4 169.4
## 2
127.6 112.2
## 3
116.6 118.8
## 4
149.6 154.0
## 5
129.8 129.8
## 6
167.2 167.2
(c)
4
height.metric <- Davis[, c("height", "repht")]
head(height.metric)
##
height repht
## 1
182
180
## 2
161
159
## 3
161
158
## 4
177
175
## 5
157
155
## 6
170
165
(d)
height.imp <- round(height.metric / 2.54, 1)
head(height.imp)
##
height repht
## 1
71.7
70.9
## 2
63.4
62.6
## 3
63.4
62.2
## 4
69.7
68.9
## 5
61.8
61.0
## 6
66.9
65.0
(e)
Davis.imp <- cbind(Davis$sex, weight.imp, height.imp)
colnames(Davis.imp) <- c(
"sex", "rec.weight", "rep.weight", "rec.height", "rep.height")
head(Davis.imp)
##
sex rec.weight rep.weight rec.height rep.height
## 1
M
169.4
169.4
71.7
70.9
## 2
F
127.6
112.2
63.4
62.6
## 3
F
116.6
118.8
63.4
62.2
## 4
M
149.6
154.0
69.7
68.9
## 5
F
129.8
129.8
61.8
61.0
## 6
M
167.2
167.2
66.9
65.0
5
(f)
missing_counts <- colSums(is.na(Davis.imp))
missing_counts
##
sex rec.weight rep.weight rec.height rep.height
##
0
0
17
0
17
(g)
rows_with_missing <- sum(rowSums(is.na(Davis.imp)) > 0)
rows_with_missing
## [1] 19
(h)
missing_sex <- Davis.imp[which(rowSums(is.na(Davis.imp)) > 0), "sex"]
missing_sex
##
[1] M F M F F F M F F F F F F F M F F M M
## Levels: F M
Problem 3
(a)
planets <- data.frame(
name = c("Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"),
distance = c(0.39, 0.72, 1, 1.52, 5.2, 9.54, 19.18, 30.06),
type = c("Terrestrial", "Terrestrial",
"Terrestrial", "Terrestrial", "Gas", "Gas", "Gas", "Gas"),
diameter = c(0.382, 0.949, 1, 0.532, 11.209, 9.449, 4.007, 3.883),
rotation = c(58.64, -243.02, 1, 1.03, 0.41, 0.43, -0.72, 0.67),
rings = c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE),
moons = rep(c("0","1","more than 1"), c(2,1,5))
)
planets
6
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
##
name distance
type diameter rotation rings
moons
## 1 Mercury
0.39 Terrestrial
0.382
58.64 FALSE
0
## 2
Venus
0.72 Terrestrial
0.949
-243.02 FALSE
0
## 3
Earth
1.00 Terrestrial
1.000
1.00 FALSE
1
## 4
Mars
1.52 Terrestrial
0.532
1.03 FALSE more than 1
## 5 Jupiter
5.20
Gas
11.209
0.41
TRUE more than 1
## 6
Saturn
9.54
Gas
9.449
0.43
TRUE more than 1
## 7
Uranus
19.18
Gas
4.007
-0.72
TRUE more than 1
## 8 Neptune
30.06
Gas
3.883
0.67
TRUE more than 1
(b)
earthDiam<- planets[which(planets$name == "Earth"), "diameter"]
diam <- planets$diameter < 2*earthDiam
diam1 <- planets[diam,]
diam1
##
name distance
type diameter rotation rings
moons
## 1 Mercury
0.39 Terrestrial
0.382
58.64 FALSE
0
## 2
Venus
0.72 Terrestrial
0.949
-243.02 FALSE
0
## 3
Earth
1.00 Terrestrial
1.000
1.00 FALSE
1
## 4
Mars
1.52 Terrestrial
0.532
1.03 FALSE more than 1
(c)
oppRot <- planets[planets$rotation < 0, ]
distRot <- oppRot$distance
distRot
## [1]
0.72 19.18
(d)
largerDiam <- planets$diameter > earthDiam
diam2 <- planets[largerDiam, c("name", "distance", "type")]
diam2
##
name distance type
## 5 Jupiter
5.20
Gas
## 6
Saturn
9.54
Gas
## 7
Uranus
19.18
Gas
## 8 Neptune
30.06
Gas
7
(e)
moreOneMoon <- planets$diameter > earthDiam
moon2 <- planets[largerDiam, c("name", "rings")]
moon2
##
name rings
## 5 Jupiter
TRUE
## 6
Saturn
TRUE
## 7
Uranus
TRUE
## 8 Neptune
TRUE
8