How to find the max value across multiple columns in R?

In R, the max value across multiple columns can be found using the apply() function. The apply() function takes a matrix or array, along with a margin argument (1 for columns, 2 for rows), and a max() function as arguments and returns a vector containing the maximum value from each column or row. This can be used to quickly find the maximum value from multiple columns in a data frame.


We can use the pmax() function to find the max value across multiple columns in R. This function uses the following syntax:

pmax(…, na.rm = FALSE)

where:

  • : A list of vectors
  • na.rm: A logical indicating whether missing values should be removed. Default is FALSE.

This tutorial provides several examples of how to use this function in practice using the following data frame:

#create data frame
df <- data.frame(player=c('A', 'B', 'C', 'D', 'E', 'F', 'G'),
                 points=c(28, 17, 19, 14, 23, 26, 5),
                 rebounds=c(5, 6, 4, 7, 14, 12, 9),
                 assists=c(10, 13, 7, 8, 4, 5, 8))

#view DataFrame
df

  player points rebounds assists
1      A     28        5      10
2      B     17        6      13
3      C     19        4       7
4      D     14        7       8
5      E     23       14       4
6      F     26       12       5
7      G      5        9       8

Example 1: Find the Max Across Specific Columns

The following code shows how to find the max value across the points and rebounds columns in a data frame:

#find max values in each row across points and rebounds columns
pmax(df$points, df$rebounds)

[1] 28 17 19 14 23 26  9

Example 2: Add A New Column Containing the Max Value

The following code shows how to add a new column to the data frame that contains the max value across the points and rebounds columns:

#add new column that contains max values across points and rebounds columns
df$max_points_rebs <- pmax(df$points, df$rebounds)

#view data frame
df

  player points rebounds assists max_points_rebs
1      A     28        5      10              28
2      B     17        6      13              17
3      C     19        4       7              19
4      D     14        7       8              14
5      E     23       14       4              23
6      F     26       12       5              26
7      G      5        9       8               9

Example 3: Add Several New Columns Containing Max Values

The following code shows how to add several new columns to the data frame that contain the max values across different groups of columns:

#add new column that contains max values across points and rebounds columns
df$max_p_r <- pmax(df$points, df$rebounds)

#add new column that contains max values across rebounds and assists columns
df$max_r_a <- pmax(df$rebounds, df$assists)

#view data frame
df

  player points rebounds assists max_p_r max_r_a
1      A     28        5      10      28      10
2      B     17        6      13      17      13
3      C     19        4       7      19       7
4      D     14        7       8      14       8
5      E     23       14       4      23      14
6      F     26       12       5      26      12
7      G      5        9       8       9       9

x