max

Basics

max is a simple function that returns the maximum value in a vector or column in a data.frame.

Some additional applications of max include comparing the maximum values between columns, finding the maximum value of a row, and finding a string that’s alphabetically last in a list.


Examples

What is the biggest value in the vector weights?

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

What is the largest 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 we get the maximum value a vector if not all of its values are filled in?

Click to see solution

See our mean page for information on na.rm.

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

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

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