# How to Use colMax Function in R (With Example)

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.