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

前端 未结 5 414
眼角桃花
眼角桃花 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
    
    0 讨论(0)
  • 2020-12-18 01:09

    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
    
    0 讨论(0)
  • 2020-12-18 01:18

    With dplyr 1.0.0

    df %>% rowwise() %>% 
      mutate(max = max(x, y, z))
    
    0 讨论(0)
  • 2020-12-18 01:21

    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.

    0 讨论(0)
  • 2020-12-18 01:26

    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
    
    0 讨论(0)
提交回复
热议问题