Question 1
numbers <- (1:50)
for(i in 1:50) {
results.i <- i
numbers[i] <- results.i
}
print(numbers)
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
## [47] 47 48 49 50
Question 2 A
current.sum <- 0
for(i in (0:1000)) {
current.sum <- current.sum + i
}
print(current.sum)
## [1] 500500
Question 2 B
current.sum <- 0
for(i in seq(0, 1000, by = 2)) {
current.sum <- current.sum + i
}
print(current.sum)
## [1] 250500
Question 2 C
sum(seq(0, 1000))
## [1] 500500
sum(seq(0, 1000, by = 2))
## [1] 250500
Question 3 A
survey.clean <- data.frame(
"participant" = c(1, 2, 3, 4, 5, 6),
"q1" = c(5, 3, 2, 7, 11, 0),
"q2" = c(4, 2, 2, 5, -10, 99),
"q3" = c(-4, -3, 4, 2, 9, 10),
"q4" = c(-30, 5, 2, 23, 4, 2),
"q5" = c(88, 4, -20, 2, 4, 2)
)
for (i in c(2:6)) {
y <- survey.clean[, i]
y[(y %in% c(1:5)) == F] <- NA
survey.clean[, i] <- y
}
print(survey.clean)
## participant q1 q2 q3 q4 q5
## 1 1 5 4 NA NA NA
## 2 2 3 2 NA 5 4
## 3 3 2 2 4 2 NA
## 4 4 NA 5 2 NA 2
## 5 5 NA NA NA 4 4
## 6 6 NA NA NA 2 2
Question 3 B
survey.clean$invalid.answers <- rep(NA, 6)
questions.i <- grep("^q", names(survey.clean))
for(i in (1:6) ) {
part.i <- survey.clean[i, questions.i ]
na.values <- sum(is.na(part.i))
survey.clean$invalid.answers[i] <- na.values
}
survey.clean
## participant q1 q2 q3 q4 q5 invalid.answers
## 1 1 5 4 NA NA NA 3
## 2 2 3 2 NA 5 4 1
## 3 3 2 2 4 2 NA 1
## 4 4 NA 5 2 NA 2 2
## 5 5 NA NA NA 4 4 3
## 6 6 NA NA NA 2 2 3
Question 4
pirates <- read.table("http://nathanieldphillips.com/wp-content/uploads/2016/01/pirates.txt",
sep = "\t", header = T, stringsAsFactors = F)
standardize.me <- function(x) {
output <- (x - mean(x)) / sd(x)
return(output)
}
head(pirates)
## id sex headband age college tattoos tchests parrots favorite.pirate
## 1 1 female yes 30 JSSFP 11 20 7 Blackbeard
## 2 2 male yes 25 CCCC 15 6 3 Anicetus
## 3 3 male yes 25 CCCC 12 5 2 Jack Sparrow
## 4 4 male yes 29 JSSFP 12 0 1 Edward Low
## 5 5 female yes 31 CCCC 17 11 10 Anicetus
## 6 6 male yes 30 JSSFP 12 2 2 Jack Sparrow
## sword.type sword.time eyepatch beard.length fav.pixar
## 1 cutlass 0.83 1 0 Up
## 2 cutlass 0.03 1 16 Toy Story 2
## 3 cutlass 1.44 0 21 Cars
## 4 cutlass 0.18 1 21 The Incredibles
## 5 cutlass 0.64 1 0 Inside Out
## 6 sabre 13.90 1 22 Inside Out
pirates.z <- data.frame(pirates$age, pirates$tattoos, pirates$tchests, pirates$parrots, pirates$sword.time, pirates$beard.length)
head(pirates.z)
## pirates.age pirates.tattoos pirates.tchests pirates.parrots
## 1 30 11 20 7
## 2 25 15 6 3
## 3 25 12 5 2
## 4 29 12 0 1
## 5 31 17 11 10
## 6 30 12 2 2
## pirates.sword.time pirates.beard.length
## 1 0.83 0
## 2 0.03 16
## 3 1.44 21
## 4 0.18 21
## 5 0.64 0
## 6 13.90 22
for(i in (1:6)) {
pirates.z[, i ] <- standardize.me(pirates.z[, i])
}
head(pirates.z)
## pirates.age pirates.tattoos pirates.tchests pirates.parrots
## 1 0.4705692 0.4837266 1.8316209 1.6147531
## 2 -0.4326347 1.6649880 -0.1670301 0.1411021
## 3 -0.4326347 0.7790419 -0.3097909 -0.2273107
## 4 0.2899284 0.7790419 -1.0235949 -0.5957234
## 5 0.6512100 2.2556187 0.5467738 2.7199914
## 6 0.4705692 0.7790419 -0.7380733 -0.2273107
## pirates.sword.time pirates.beard.length
## 1 -0.16245384 -0.9591265
## 2 -0.25404809 0.6028098
## 3 -0.09261323 1.0909148
## 4 -0.23687417 1.0909148
## 5 -0.18420748 -0.9591265
## 6 1.33396715 1.1885359
#We predict a mean of zero and a standard deviation of 1.
mean. <- NULL
sd. <- NULL
for(i in (1:6)) {
mean. <- c(mean., mean(pirates.z[, i]))
sd. <- c(sd., sd(pirates.z[, i]))
}
print(mean.)
## [1] 9.581778e-17 -3.521380e-17 -1.107621e-18 -1.285316e-17 7.051105e-18
## [6] 7.438765e-17
print(sd.)
## [1] 1 1 1 1 1 1
#Yeeeah:)
Question 5
shipauction <- read.table("http://nathanieldphillips.com/wp-content/uploads/2016/01/auction.txt",
sep = "\t", header = T, stringsAsFactors = F)
aggregate(price ~ cannons, data = shipauction, FUN = mean)
## cannons price
## 1 2 273.0686
## 2 4 226.6176
## 3 6 450.7007
## 4 8 566.2791
## 5 10 739.4052
## 6 12 832.2190
## 7 14 1022.2750
## 8 16 1254.7119
## 9 18 1426.8649
## 10 20 1423.1250
Question 6
par(mfrow = c(4, 4))
ship.condition <- unique(shipauction$condition)
for(condition.i in ship.condition) {
data.temp <- subset(shipauction, condition == condition.i)
hist(data.temp$price,
main = condition.i,
xlab = "price")
meag <- mean(shipauction$price)
abline(v = meag, col = "blue", lwd = 2)
}
