Tutorial Exercises Week 2

Question 1

Use R to calculate the principal cubed root of 64, \sqrt[3]{64}.

Question 2

Use R to calculate \ln(e^5).

Question 3

Use R to calculate \log_4(64).

That is, take the log of 64 to the base 4.

Question 4

In R we create vectors with the c() function (the combine function). In a vector, all elements need to have the same type (like numerical, logical, character). If we combine elements of different types into a vector, the c() function will force elements to a common type.

What types are the following vectors?

  • c(1, 2, 3, TRUE, FALSE)
  • c(1, 2, "3")
  • c(TRUE, FALSE, "Yes", "No")

Question 5

Consider the sequence \left(1.0, 1.2, 1.4, 1.6, ..., 100\right).

  • How many numbers are in the sequence?
  • What is the 100th number in the sequence?
  • What is the median value in the sequence?

Question 6

Create the sequence:

\left(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, ..., 98, 98, 99, 99, 100, 100\right)

Assign this sequence to the variable x.

Write a command to get the subset of this sequence with values exceeding 60. The output should be: \left(61, 61, 62, 62, ..., 99, 99, 100, 100\right)

What is the average of this subsequence?

Question 7

Using the example logical vectors a and b from the book:

a <- c(TRUE, TRUE, FALSE, FALSE)
b <- c(TRUE, FALSE, TRUE, FALSE)

Which command returns the elements where a and b are not both TRUE?

Question 8

Download the file rotterdam-airbnb.csv.

This contains data on Rotterdam Airbnb listings.

Read it into R. What is the average price of a night’s stay in the data?

If the data is loaded in R as df, you can get the vector of prices with the command df$price.