<- function(v, n) {
every_nth # Your code here
}
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\)).
Show Solution
<- function(v, n) {
every_nth c(TRUE, rep(FALSE, n - 1))]
v[
}
# Alternate Solution
<- function(v, n) {
every_nth seq(1, length(v), n)]
v[ }
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.
<- function(v, n) {
nth_minmax_modification # Your code here
}
Show Solution
<- function(v, n) {
nth_minmax_modification <- max(v)
largest <- min(v)
smallest <- c(TRUE, rep(FALSE, n - 1))
pattern <- largest
v[pattern] !pattern] <- smallest
v[
v }
2. Inclusion, Exclusion
Write a function that returns a vector that excludes all elements equal to some specified value, to_exclude
.
<- function(v, to_exclude) {
exclude # Your code here
}
Show Solution
<- function(v, to_exclude) {
exclude != to_exclude]
v[v }
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.
<- function(v, to_exclude) {
exclude # Your code here
}
Show Solution
<- function(v, to_exclude) {
exclude !(v %in% to_exclude)]
v[
}# 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
.
<- function(v, to_include) {
include # Your code here
}
Show Solution
<- function(v, to_include) {
include %in% to_include]
v[v }
3. Palindrome
A palindrome is a sequence that is read the same forwards and backwards. Return whether a specified vector is a palindrome.
<- function(v) {
is_palindrome # Your code here
}
Show Solution
<- function(v) {
is_palindrome all(rev(v) == v)
}
# If you didn't know about the `all` function, you could have used `sum`.
<- function(v) {
is_palindrome 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 |
<- function(v) {
to_roman # Your code here
}
Show Solution
<- function(v) {
to_roman <- c("I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X")
ref
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.
<- function(inches) {
inches_to_cm # Your code here
}
Show Solution
<- function(inches) {
inches_to_cm * 2.54
inches }
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}.\]
<- function(fahrenheit) {
fahrenheit_to_celsius # Your code here
}
Show Solution
<- function(fahrenheit) {
fahrenheit_to_celsius - 32) / 1.8
(fahrenheit }
6. Ranges
Write a function that removes all values outside of the given range (inclusive).
<- function(v, min, max) {
in_range # Your code here
}
Show Solution
<- function(v, min, max) {
in_range >= min & v <= max]
v[v }
Now write a function that turns all values strictly outside the range into replacement
.
<- function(v, min, max, replacement) {
replace_outside_range # Your code here
}
Show Solution
<- function(v, min, max, replacement) {
replace_outside_range < min | v > max] <- replacement
v[v
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
.
<- function(v, min, max) {
restrict_to_range # Your code here
}
Show Solution
<- function(v, min, max) {
restrict_to_range < min] <- min
v[v > max] <- max
v[v
v }