Skip to content

Latest commit

 

History

History
219 lines (156 loc) · 4.86 KB

vector_matrix.md

File metadata and controls

219 lines (156 loc) · 4.86 KB

Exercise 1: Our first vector

Create a numeric vector that goes from 1 to 20. Then, divide it by 2 and store the results to another vector, called divided_by_two. Finally, make a third logical vector storing which values of divided_by_two are greater than 5.

print()
Answer
a <- c(1:20)
divided_by_two <- a / 2
greater_than_five <- divided_by_two > 5

Exercise 2: Appending truth

Extend the last vector of exercise #1, by adding the following values: TRUE, TRUE, FALSE

Answer
greater_than_five <- c(greate_than_five, TRUE, TRUE, FALSE)

Exercise 3: The longest vector

Write a function called get_longest_vector that takes two vectors as arguments. The function should check which is the longest vector and return its name. If there is a tie, the second vector wins.

Answer
get_longest_vector <- function(v1, v2){
 if(length(v1) > length(v2)){
   return(paste(substitute(v1), 'is the winner vector'))
 }else{
   return(paste(substitute(v2), 'is the winner vector'))
 }
}

You may have noticed I have used the function `substitute`. It returns the
actual name of the object containing v1 and v2 respectively. 

Exercise 4: Random minimum and maximum

Use the functions min() and max() to find the minimum and maximum values of the following vector:

my_random_vector <- runif(n=200, min=0, max=400000000)
Answer
max_value <- max(my_random_vector)
min_value <- min(my_random_vector)

Exercise 5: basic statistics

Use the functions mean(), median() and sd() to calculate the mean, median and standard deviation of my_random_vector.

Answer
mean_value   <- mean(my_random_vector)
median_value <- median(my_random_vector)
sd_value     <- sd(my_random_vector)

Exercise 6: greater than average

Use the previous random vector and the median you calculated to create a logic vector of the values in random_vector greater than the median value.

Answer
median_value <- median(my_random_vector)

greater_than_average <- random_vector > median_value

Exercise 7: Mixing types

Create the following vector:

mixed_vector <- c(TRUE, 3, "KRAS", 4.00)

Check its class and answer the following questions:

  1. Which data type are you getting?
  2. Why?
Answer
  1. Character
  2. Because it is the only possibility. Every value in mixed_vector can be represented as character. There is no ot her posibility.

Exercise 8

Create a 4x6 numeric matrix called my_matrix and fill it with values ranging from 1 to 24.

Answer
my_matrix <- matrix(1:24, nrow=4, ncol=6)

Exercise 9

Multiply the previous matrix by 4. Print the result.

Answer
my_matrix <- my_matrix * 4
print(my_matrix)

Exercise 10

Check which cells of the previous matrix are greater than 20. Store the results in a logical matrix. Print the result.

Answer
my_logic_matrix <- my_matrix > 20
print(my_logic_matrix)

Exercise 11

Create the following matrix:

too_many_cells <- matrix(1:12, nrow = 5, ncol = 4)

And answer the following questions:

  1. Are you getting a warning? Why?
  2. How did R solve the aforementioned warning?
Answer
  1. Yes, a warning is issued because the matrix has more cells than the amount of values we supplied.
  2. R recicled our vector! It repeated values from 1:12 until it filled the whole matrix.

Exercise 12

Create the following matrix:

all_ones <- matrix(1:2, nrow = 5, ncol = 4)

And answer the following questions:

  1. Are you getting a warning?
  2. We have 20 cells and only one value. What just happened?
Answer
  1. No
  2. R recicled our vector again. But It only complains If our number of cells is not divisible by the amount of values we have. Otherwise, It will repeat the whole vector as many times as necessary until it fills the matrix. In this case 20 cells by 2 values equals 10 repetitions.

Exercise 13: An introduction to logical indexing

Use the logic vector of exercise #6 to subset the values of random_vector which are greater than the median value.

Answer
greater_than_average <- random_vector > median_value

subset_vector <- my_random_vector[greater_than_average]

Try to guess what happened here. If you are clueless, don't worry, since we will
be reviewing logical indexing in the next session.