Finding the maximum value for each row among 3 columns in R

r max
18,121

Solution 1

You can use the apply function for this like so:

df$max<-apply(X=df, MARGIN=1, FUN=max)

The MARGIN=1 argument indicated that for every row in X you wish to apply the function in FUN. If you use MARGIN=2 it will be by column or MARGIN=c(1,2) it will be both rows and columns.

Solution 2

Use data.table :)

library(data.table)
x = c(1,2,3,4,5 ) 
y = c(2,3,3,1,1 ) 
z = c(4,3,2,1,1 ) 
dt<-data.table(x,y,z)
dt[, max:=pmax(x,y,z)]
dt

Solution 3

Try:

 df$max <- do.call(`pmax`, df)
 df
 #  x y z max
 #1 1 2 4   4
 #2 2 3 3   3
 #3 3 3 2   3
 #4 4 1 1   4
 #5 5 1 1   5

Benchmarks

 set.seed(49)
 df <- as.data.frame(matrix(sample(0:20, 1e5*3,replace=TRUE), ncol=3))
 f1 <- function() df$max <- apply(df, 1, max)
 f2 <- function() df$max <- do.call(`pmax`, df)
 f3 <- function() setDT(df)[, max:=pmax(V1,V2,V3)]

 library(microbenchmark)
 microbenchmark(f1(), f2(),f3(), unit="relative", times=25)
 #Unit: relative
 # expr       min        lq    median        uq      max neval
 # f1() 48.143635 48.287875 46.031638 32.868138 8.922203    25
 # f2()  1.269581  1.373479  1.654625  2.324896 1.182107    25
 # f3()  1.000000  1.000000  1.000000  1.000000 1.000000    25

Solution 4

If John posts accept his, but just to show the result his comment does work

x = c(1,2,3,4,5 ) 
y = c(2,3,3,1,1 ) 
z = c(4,3,2,1,1 ) 
df<-data.frame(x,y,z)

df$max<-apply(df, 1, max)
df$max
#[1] 4 3 3 4 5


df
#x y z max
#1 2 4   4
#2 3 3   3
#3 3 2   3
#4 1 1   4
#5 1 1   5

Solution 5

With dplyr 1.0.0

df %>% rowwise() %>% 
  mutate(max = max(x, y, z))
Share:
18,121
GabyLP
Author by

GabyLP

Updated on July 27, 2022

Comments

  • GabyLP
    GabyLP almost 2 years

    I need to calculate the maximum value for each row among 3 columns.

    A table could be:

    x = c(1,2,3,4,5 ) 
    y = c(2,3,3,1,1 ) 
    z = c(4,3,2,1,1 ) 
    df<-data.frame(x,y,z)
    

    I need to get:

        x   y   z   max
    1   1   2   4   4
    2   2   3   3   3
    3   3   3   2   3
    4   4   1   1   4
    5   5   1   1   5
    

    I tried:

    df$max<-max(x, y,z)
    

    But I get:

      x y z max
    1 1 2 4   5
    2 2 3 3   5
    3 3 3 2   5
    4 4 1 1   5
    5 5 1 1   5
    

    So, how can I do this correctly?

  • AdIan
    AdIan over 3 years
    what about the second largest one of each row?