R Parallel Summaries
Parallel summaries compare values position by position across vectors. pmin and pmax pick the smaller and larger value at each position.
The smaller and larger at each position
low and high hold three values each. pmin picks the smaller value at each position and pmax picks the larger one.
low <- c(1, 9, 4)
high <- c(5, 2, 6)
print(pmin(low, high))
print(pmax(low, high))Across three vectors
Three vectors line up by position. pmin and pmax read all three values at each position before picking.
a <- c(8, 3, 7)
b <- c(5, 9, 2)
c <- c(6, 4, 10)
print(pmin(a, b, c))
print(pmax(a, b, c))Positions where a test is true
which reads the result of a test and returns the positions marked TRUE. The two tests pick the high scores and the low scores.
scores <- c(45, 82, 67, 91, 58)
print(which(scores > 70))
print(which(scores < 60))The position of an extreme
which.max returns the position of the largest value and which.min returns the position of the smallest. A tied largest value reports its first position.
scores <- c(2, 8, 5)
print(which.max(scores))
print(which.min(scores))
ties <- c(4, 9, 9, 3)
print(which.max(ties))Example
first <- c(62, 88, 75)
second <- c(70, 81, 90)
best <- pmax(first, second)
print(pmin(first, second))
print(best)
print(which(best > 85))
print(which.max(best))Key points
- pmin takes the smaller value at each position across its vectors.
- pmax takes the larger value at each position across its vectors.
- pmin and pmax each return a vector with one value for each position.
- which turns a logical test into the positions where the test is TRUE.
- which.max returns the position of the largest value in a vector.
- which.min returns the position of the smallest value in a vector.
In this example, first and second hold two attempts for three learners. pmin gives the weaker attempt at each position and pmax gives the stronger one, which finds the strong results above 85, and which.max points at the strongest result of all.
Note
When the largest value appears twice, which.max returns the first of the tied positions.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท rdrr.io R Snippets
Exercise
What does which.max(c(2, 8, 5)) print?