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

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

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.


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

Tags:

R

Max