min

min is a simple function which returns the minimum value in a vector or column in a data.frame. This can be used to compare the minimum values between columns, find the minimum value of a row, or find a string that’s alphabetically first in a list.

Examples

What is the smallest value in the vector weights?

Click to see solution
weights <- c(147, 280, 180, 190, 145)
min(weights)
[1] 145

What is the smallest response over an experiment data.frame with trial1, trial2, and trial3 as its trial columns?

Click to see solution
max(c(experiment$trial1, experiment$trial2, experiment$trial3))
[1] 87

How do I get the minimum of the values in a vector when some of the values are: NA, NaN?

Click to see solution
vec <- c(NA, 45, 444, 13, 98, NA)
max(vec, na.rm = TRUE)
[1] 13

If we don’t include the na.rm = TRUE argument, the function will only return NA and we won’t get any meaningful information out of the code.

What is the first word in alphabetical order in the vector sentence?

Click to see solution
sentence <- c('I', 'thought', 'red', 'would', 'have', 'felt', 'warmer', 'in', 'the', 'summer')
min(sentence)
[1] "felt"