max

Introduction

max is one of many simple numerical functions you can use to analyze a vector in R. It returns the maximum value in a vector or column in a data.frame.

Some additional applications of this 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?

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?

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?

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

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 last word in alphabetical order in the vector sentence?

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