Q1

Container: Not necessary at this point.

onetofifty <- rep(NA, 50)
for (i in 1:50) {
  
  onetofifty[i] <- i
  
}
onetofifty
##  [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

With print option:

for (i in 1:50) {
  
  print(i)
  
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
## [1] 11
## [1] 12
## [1] 13
## [1] 14
## [1] 15
## [1] 16
## [1] 17
## [1] 18
## [1] 19
## [1] 20
## [1] 21
## [1] 22
## [1] 23
## [1] 24
## [1] 25
## [1] 26
## [1] 27
## [1] 28
## [1] 29
## [1] 30
## [1] 31
## [1] 32
## [1] 33
## [1] 34
## [1] 35
## [1] 36
## [1] 37
## [1] 38
## [1] 39
## [1] 40
## [1] 41
## [1] 42
## [1] 43
## [1] 44
## [1] 45
## [1] 46
## [1] 47
## [1] 48
## [1] 49
## [1] 50

Q2

A

add1to1000 <- 0
for (i in 1:1000) {
  
  add1to1000 <- add1to1000 + i
  
}
add1to1000
## [1] 500500

B

addeven2to1000 <- 0
for (i in seq(from = 2, to = 1000, by = 2)) {
  addeven2to1000 <- addeven2to1000 + i
}
addeven2to1000
## [1] 250500

C

sum(1:1000)
## [1] 500500
sum(seq(from = 2, to = 1000, by = 2))
## [1] 250500

Q3

Did a bit differently, just read A and startet without reading the subpoints.

survey <- 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 2:6) {
  y <- survey[, i]
  y[(y %in% c(1,2,3,4,5)) == F] <- NA
  survey[,i] <- y
}

survey
##   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

B

x <- survey
counter.i <- 0
invalid.answers <- rep(NA, (nrow(x)))
for(i in 1:(nrow(x))) {
  for(j in 1:ncol((x))) {
    if (is.na(x[i,j])) {
      counter.i <- counter.i + 1
    }
  }
  invalid.answers[i] <- counter.i
  counter.i <- 0
}
invalid.answers
## [1] 3 1 1 2 3 3
cbind(survey, invalid.answers)
##   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

Q4

pirates <- read.csv("http://nathanieldphillips.com/wp-content/uploads/2016/01/pirates.txt", sep = "\t")

standardize.me <- function(x) {
  output <- (x - mean(x))/ sd(x)
  
  return(output)
  
} 

for(i in c(4,6,7,8,11,12,13)) {
  y <- pirates[,i]
  y <- standardize.me(y)
  pirates[,i] <- y
}
head(pirates)
##   id    sex headband        age college   tattoos    tchests    parrots
## 1  1 female      yes  0.4705692   JSSFP 0.4837266  1.8316209  1.6147531
## 2  2   male      yes -0.4326347    CCCC 1.6649880 -0.1670301  0.1411021
## 3  3   male      yes -0.4326347    CCCC 0.7790419 -0.3097909 -0.2273107
## 4  4   male      yes  0.2899284   JSSFP 0.7790419 -1.0235949 -0.5957234
## 5  5 female      yes  0.6512100    CCCC 2.2556187  0.5467738  2.7199914
## 6  6   male      yes  0.4705692   JSSFP 0.7790419 -0.7380733 -0.2273107
##   favorite.pirate sword.type  sword.time   eyepatch beard.length
## 1      Blackbeard    cutlass -0.16245384  0.6951229   -0.9591265
## 2        Anicetus    cutlass -0.25404809  0.6951229    0.6028098
## 3    Jack Sparrow    cutlass -0.09261323 -1.4371559    1.0909148
## 4      Edward Low    cutlass -0.23687417  0.6951229    1.0909148
## 5        Anicetus    cutlass -0.18420748  0.6951229   -0.9591265
## 6    Jack Sparrow      sabre  1.33396715  0.6951229    1.1885359
##         fav.pixar
## 1              Up
## 2     Toy Story 2
## 3            Cars
## 4 The Incredibles
## 5      Inside Out
## 6      Inside Out
pirates.z <- cbind(pirates[,4], pirates[,6],pirates[,7],pirates[,8],pirates[,11],pirates[,12],pirates[,13])

means.standardized <- rep(NA, 7)
for(i in 1:7) {
  x <- pirates.z[,i]
  y <- mean(x)
  means.standardized[i] <- y
}
means.standardized
## [1]  9.581778e-17 -3.521380e-17 -1.107621e-18 -1.285316e-17  7.051105e-18
## [6] -1.247169e-16  7.438765e-17
sd.standardized <- rep(NA, 7)
for(i in 1:7) {
  x <- pirates.z[,i]
  y <- sd(x)
  sd.standardized[i] <- y
}
sd.standardized
## [1] 1 1 1 1 1 1 1

Q5

ships <- read.csv("http://nathanieldphillips.com/wp-content/uploads/2016/01/auction.txt", sep = "\t")
head(ships)
##   cannons rooms  age condition color weight   style price sold  jbb
## 1      16     5 50.7         8 black   9405  modern   394    1  600
## 2      10    31 61.9         1 brown   9208 classic   399    1  500
## 3      10    12 36.7         5   red   7285  modern     0    0  400
## 4      12    56 40.8         3 black  12678  modern  2158    1 3100
## 5      10    21 57.8         6 black   8282 classic   491    1  400
## 6      10    19 57.3         5 brown   7929 classic     0    0  200
mean.price <- rep(NA, max(ships$cannons))

for(i in 1:max(ships$cannons)) {
  if (i %in% ships$cannons) {
    x <- mean(ships$price[ships$cannons == i])
    mean.price[i] <- x
    
    
  }
}
mean.price 
##  [1]        NA  273.0686        NA  226.6176        NA  450.7007        NA
##  [8]  566.2791        NA  739.4052        NA  832.2190        NA 1022.2750
## [15]        NA 1254.7119        NA 1426.8649        NA 1423.1250