Vector Introduction

This page is a collection of practice problems related to vectors, particularly vector subsetting and vectorization. The purpose here is to practice these concepts, so do not use control statements (if/else/for/while).

Do not worry about error checking for these problems. That is, assume the user of your function has passed in valid arguments.

1. Intervals

Write a function that returns every \(n\)th element from a vector (\(n \geq 1\)).

every_nth <- function(v, n) {
  # Your code here
}
Show Solution
every_nth <- function(v, n) {
  v[c(TRUE, rep(FALSE, n - 1))] 
}

# Alternate Solution
every_nth <- function(v, n) {
  v[seq(1, length(v), n)]
}

Now write a function that sets every \(n\)th element of a vector to the maximum value in the vector. The rest should be set to the minimum.

nth_minmax_modification <- function(v, n) {
  # Your code here
}
Show Solution
nth_minmax_modification <- function(v, n) {
  largest <- max(v)
  smallest <- min(v)
  pattern <- c(TRUE, rep(FALSE, n - 1))
  v[pattern] <- largest
  v[!pattern] <- smallest
  v
}

2. Inclusion, Exclusion

Write a function that returns a vector that excludes all elements equal to some specified value, to_exclude.

exclude <- function(v, to_exclude) {
  # Your code here
}
Show Solution
exclude <- function(v, to_exclude) {
  v[v != to_exclude]
}

Now write a function that returns a vector that excludes all elements equal to any of the elements in the vector to_exclude.

Hint: you may find the %in% operator helpful.

exclude <- function(v, to_exclude) {
  # Your code here
}
Show Solution
exclude <- function(v, to_exclude) {
  v[!(v %in% to_exclude)]
}
# Q: Does this solution also work for the previous question?

Now do the same, but this time include only the elements equal to any of the elements in the vector to_include.

include <- function(v, to_include) {
  # Your code here
}
Show Solution
include <- function(v, to_include) {
  v[v %in% to_include]
}

3. Palindrome

A palindrome is a sequence that is read the same forwards and backwards. Return whether a specified vector is a palindrome.

is_palindrome <- function(v) {
  # Your code here
}
Show Solution
is_palindrome <- function(v) {
  all(rev(v) == v)
}

# If you didn't know about the `all` function, you could have used `sum`.

is_palindrome <- function(v) {
  sum(rev(v) == v) == length(v)
}

# Follow up: Try doing it by only comparing the two halves of the vector to each
# other.

4. Roman Numerals

Write a function that takes a vector v of numbers and returns a vector where each number in v has been converted to its roman numeral equivalent.

Number Roman Numeral
1 I
2 II
3 III
4 IV
5 V
6 VI
7 VII
8 VIII
9 IX
10 X
to_roman <- function(v) {
  # Your code here
}
Show Solution
to_roman <- function(v) {
  ref <- c("I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X")
  ref[v]
}

5. Unit Conversion

Write a function that converts a vector of inches to centimeters. Note that 1 inches is equivalent to 2.54 centimeters.

inches_to_cm <- function(inches) {
  # Your code here
}
Show Solution
inches_to_cm <- function(inches) {
  inches * 2.54
}

Write a function that converts a vector of temperatures from Fahrenheit to Celsius. The conversion formula is

\[^{\circ}C = \frac{^{\circ}F - 32}{1.8}.\]

fahrenheit_to_celsius <- function(fahrenheit) {
  # Your code here
}
Show Solution
fahrenheit_to_celsius <- function(fahrenheit) {
  (fahrenheit - 32) / 1.8
}

6. Ranges

Write a function that removes all values outside of the given range (inclusive).

in_range <- function(v, min, max) {
  # Your code here
}
Show Solution
in_range <- function(v, min, max) {
  v[v >= min & v <= max]
}

Now write a function that turns all values strictly outside the range into replacement.

replace_outside_range <- function(v, min, max, replacement) {
  # Your code here
}
Show Solution
replace_outside_range <- function(v, min, max, replacement) {
  v[v < min | v > max] <- replacement
  v
}

Now write a function that instead brings all values into the range. If a value is less than min, it should be set to min. If a value is greater than max, it should be set to max.

restrict_to_range <- function(v, min, max) {
  # Your code here
}
Show Solution
restrict_to_range <- function(v, min, max) {
  v[v < min] <- min
  v[v > max] <- max
  v
}