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

前端 未结 5 417
眼角桃花
眼角桃花 2020-12-18 00:22

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<         


        
5条回答
  •  春和景丽
    2020-12-18 01:05

    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
    

提交回复
热议问题