Question 1 (5 points)
Do the following in R:
- Assign 8 to
p
- Assign 6 to
q
- Swap the values of
p
and q
. You are not allowed to directly assign values. You have to “swap” them!
# Write your answer here
Question 2 (10 points)
- Create a vector of the populations of the 10 largest cities in Turkey.
- Name your vector with the names of the cities.
- Print the names of the cities that have a population between 2 million and 3 million.
# Write your answer here
Question 3 (10 points)
- Create a matrix as follows:
- First consists of numbers: {1,2,3,4,5,6}
- Second row consists of numbers: {2,4,6,8,10,12}
- Third row consists of numbers: {1,3,5,7,9,11}
- Fourth row consists of the sum of the second and third rows
- Fifth row consists of the division of the fifth row with the first row
- Swap the columns of the matrix so that the first row reads: {1,3,5,2,4,6}
# Write your answer here
Question 4 (10 points)
- Create a factor from the following vector:
{"red", "red", "blue", "brown", "green", "blue", "red", "green", "green", "brown", "red", "blue"}
- Display the frequencies of each factor value (level)
- Re-name
"red"
as "purple"
- Display the number of “purples”
# Write your answer here
Question 5 (20 points)
- Create a data frame for the following girls. You must choose the correct column types:
- Canan is 24 years old, blonde, 170cm and 56kgs. She is married.
- Deniz is 35 years old, has brown hair, 173cm and 61kgs. She is married.
- Eda is 21 years old, has brown hair, 156cm and 45kgs. She is not married.
- Fatma is 40 years old, has black hair, 164cm and 60kgs. She is married.
- Gonca is 33 years old, blonde, 182cm and 65kgs. She is not married.
- Hilal is 45 years old, has black hair, 165cm and 58kgs. She is married.
- Lale is 38 years old, has black hair, 175cm and 59kgs. She is not married.
- Mine is 28 years old, has brown hair, 190cm and 71kgs. She is not married.
- Answer the following questions based on this dataframe:
- What is the average age of the group?
- How many girls are above the average height?
- What is the most frequent hair color?
- What is the average height of girls above 60kgs?
- Compare the height/weight ratio of married and single girls. Which is higher?
# Write your answer here
Question 6 (15 points)
- Given the below vector, compute its mean without using any functions.
# 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
Question 7 (20 points)
- Write a function that takes two numeric vectors and returns a matrix as follows:
# 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
Question 8 (20 points)
- Write a function that takes a numeric vector
vec
and a numeric variable val
, and returns TRUE
if val
exists in vec
, and otherwise returns FALSE
. You are not allowed to use %in%
or any other functions present in R.
# Write your answer here