How can the colMax Function be used in R? (With Example)

The colMax function in R is used to find the maximum value in a specified column of a data frame. This function is useful for analyzing and summarizing data, as it allows users to quickly identify the highest value in a particular column. For example, if we have a data frame with information on the sales of different products, we can use the colMax function to find the maximum sales figure and determine which product had the highest sales. This function is especially useful when dealing with large datasets, as it saves time and effort in manually searching for the maximum value.


By default, base R includes the following functions:

  • colMeans: Calculates the mean of each column in a data frame
  • colSums: Calculates the sum of each column in a data frame

However, there does not exist a colMax function to calculate the max of each column in a data frame.

Fortunately, you can use the following syntax to create a colMax function in R:

colMax <- function(data) sapply(data, max, na.rm=TRUE)

Once you’ve created this function, you can then use it to calculate the max value of each column in a data frame in R.

The following examples show how to use this function in practice with the following data frame in R:

#create data frame
df <- data.frame(points=c(99, 91, 86, 88, 95),
                 assists=c(33, 28, 31, 39, 34),
                 rebounds=c(30, 28, 24, 24, 28),
                 blocks=c(1, 4, 11, 0, 2))

#view data frame
df

  points assists rebounds blocks
1     99      33       30      1
2     91      28       28      4
3     86      31       24     11
4     88      39       24      0
5     95      34       28      2 

Example 1: Use colMax to Calculate Max of All Columns

We can use the following code to calculate the max value of each column in the data frame:

#calculate max value of each column in  data frame
colMax(df)

  points  assists rebounds   blocks 
      99       39       30       11 

The output shows the max value in each column of the data frame.

For example:

  • The max value in the points column is 99.
  • The max value in the assists column is 39.

And so on.

Example 2: Use colMax to Calculate Max of Specific Columns

We can use the following code to calculate the max value for only the points and blocks columns in the data frame:

#calculate max value of 'points' and 'blocks' columns in  data frame
colMax(df[, c('points', 'blocks')])

points blocks 
    99     11  

The output shows the max value in the points and blocks columns only.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

x