Question 1 (5 points)

Do the following in R:

# Solution 1 : No extra variables used

p <- 8
q <- 6
p <- p + q
q <- p - q
p <- p - q
p 
## [1] 6
q
## [1] 8
# Solution 2 : Temporary variable used

p <- 8
q <- 6
temp <- p
p <- q
q <- temp
p 
## [1] 6
q
## [1] 8

Question 2 (10 points)

# Write your answer here

city.pops <- c(14160467, 5045083, 4061074, 2740970, 2158265, 2149260, 2079225, 1844438, 1801980, 1705774)
names(city.pops) <- c("İstanbul", "Ankara", "İzmir", "Bursa", "Antalya", "Adana", "Konya", "Gaziantep", "Şanlıurfa", "Mersin")
names(city.pops)[city.pops > 2000000 & city.pops < 3000000]
## [1] "Bursa"   "Antalya" "Adana"   "Konya"

Question 3 (10 points)

# Write your answer here

first <- 1:6
second <- first * 2
third <- second - 1
fourth <- second + third
fifth <- fourth / first
m <- matrix(c(first, second, third, fourth, fifth), nrow = 5, byrow = T)
m
##      [,1] [,2]      [,3]  [,4] [,5]      [,6]
## [1,]    1  2.0  3.000000  4.00  5.0  6.000000
## [2,]    2  4.0  6.000000  8.00 10.0 12.000000
## [3,]    1  3.0  5.000000  7.00  9.0 11.000000
## [4,]    3  7.0 11.000000 15.00 19.0 23.000000
## [5,]    3  3.5  3.666667  3.75  3.8  3.833333
m <- m[, c(1, 3, 5, 2, 4, 6)]
m
##      [,1]      [,2] [,3] [,4]  [,5]      [,6]
## [1,]    1  3.000000  5.0  2.0  4.00  6.000000
## [2,]    2  6.000000 10.0  4.0  8.00 12.000000
## [3,]    1  5.000000  9.0  3.0  7.00 11.000000
## [4,]    3 11.000000 19.0  7.0 15.00 23.000000
## [5,]    3  3.666667  3.8  3.5  3.75  3.833333

Question 4 (10 points)

# Write your answer here

f <- factor(c("red", "red", "blue", "brown", "green", "blue", "red", "green", "green", "brown", "red", "blue"))
table(f)
## f
##  blue brown green   red 
##     3     2     3     4
index.of.red <- which(levels(f) == "red")
levels(f)[index.of.red] <- "purple"
table(f)["purple"]
## purple 
##      4

Question 5 (20 points)

# Write your answer here

name <- c("Canan", "Deniz", "Eda", "Fatma", "Gonca", "Hilal", "Lale", "Mine")
age <- c(24, 35, 21, 40, 33 ,45, 38, 28)
hair.color <- c("blonde", "brown", "brown", "black", "blonde", "black", "black", "brown")
height <- c(170, 173, 156, 164, 182, 165, 175, 190)
weight <- c(56, 61, 45, 60, 65, 58, 59, 71)
married <- c(T, T, F, T, F, T, F, F)
df <- data.frame(name, age, hair.color, height, weight, married)

# What is the average age of the group?
mean(df$age)
## [1] 33
# How many girls are above the average height?
sum(df$height > mean(df$height))
## [1] 4
# What is the most frequent hair color?
names(sort(table(df$hair.color), decreasing = T))[1]
## [1] "black"
# What is the most frequent hair color? [Alternative answer]
t <- table(df$hair.color)
names(t)[which(t == max(t))]
## [1] "black" "brown"
# What is the average height of girls above 60kgs?
mean(df$height[df$weight > 60])
## [1] 181.6667
# Compare the height/weight ratio of married and single girls. Which is higher?
hw.m <- mean((df$height / df$weight)[df$married])
hw.nm <- mean((df$height / df$weight)[!df$married])
if (hw.m > hw.nm)
{
   print("Married is higher")
} else if (hw.nm > hw.m)
{
   print("Not Married is higher")   
} else
{
   print("They are equal")
}
## [1] "Not Married is higher"

Question 6 (15 points)

# Do not change the two lines below
set.seed(1024)
v <- runif(100, 1, 20) + rnorm(100, 1, 3)

# Compute the mean of v below
sum <- 0
count <- 0
for (i in v)
{
   sum <- sum + i
   count <- count + 1
}

# Computed
sum / count
## [1] 11.07399
# Actual
mean(v)
## [1] 11.07399

Question 7 (20 points)

# Example:
> a <- c(1,3,5)
> b <- c(20, 40, 60)

> c <- your_function(a, b)
> c

     [,1] [,2] [,3]
[1,]   21   41   61
[2,]   23   43   63
[3,]   25   45   65
# Write your answer here
f <- function(a, b)
{
   m <- matrix(0, nrow = length(a), ncol = length(b))
   for (r in seq_along(a))
      for (c in seq_along(b))
         m[r, c] <- a[r] + b[c]
   m
}

a <- c(1, 3, 5)
b <- c(20, 40, 60)
c <- f(a, b)
c
##      [,1] [,2] [,3]
## [1,]   21   41   61
## [2,]   23   43   63
## [3,]   25   45   65
# Alternative answer
f2 <- function(a, b)
{
   m1 <- matrix(a, nrow = length(a), ncol = length(b))
   m2 <- matrix(b, nrow = length(a), ncol = length(b), byrow = T)
   m1 + m2
}

a <- c(1, 3, 5)
b <- c(20, 40, 60)
c <- f2(a, b)
c
##      [,1] [,2] [,3]
## [1,]   21   41   61
## [2,]   23   43   63
## [3,]   25   45   65

Question 8 (20 points)

# Write your answer here
f <- function(vec, val)
{
   for (i in vec)
   {
      if (i == val)
         return(TRUE)
   }
   
   return(FALSE)
}

# Testing
f(c("a", "b", "c", "d", "e"), "d")
## [1] TRUE
f(c("a", "b", "c", "d", "e"), "f")
## [1] FALSE