rvariableshighest

How can I find the 2 highest values for each row, then add them together?


In the table below, how can I find the two highest values in each row, then add these values together?

I have a copy of the attached table in RStudio. Is there a line of code I can use to add the two highest numbers on each row, so that I can apply to this to a much bigger dataset?

Table


Solution

  • You can do a rowwise calculation where you sort the values in the specified columns and sum the highest two:

    library(dplyr)
    
    df <- data.frame(Mon = c(12,15,42,43,56,73,23),
                     Tues = c(15,14,12,75,98,79,68),
                     Wed = c(13,42,35,64,35,95,56),
                     Thur = c(23,46,32,94,78,68,35),
                     Friday = c(25,23,64,35,27,54,32))
    
    
    
    df %>% 
      rowwise() %>% 
      mutate(two_max = sum(sort(c(Mon, Tues, Wed, Thur, Friday), decreasing = TRUE)[1:2])) %>% 
      ungroup()
    

    If you don't want to specify the column names manually you can also select all numeric columns at once:

    
    df %>% 
      rowwise() %>% 
      mutate(two_max = sum(sort(c_across(where(is.numeric)), decreasing = TRUE)[1:2])) %>% 
      ungroup()
    

    Both strategies give the result:

    
    # A tibble: 7 x 6
        Mon  Tues   Wed  Thur Friday two_max
      <dbl> <dbl> <dbl> <dbl>  <dbl>   <dbl>
    1    12    15    13    23     25      48
    2    15    14    42    46     23      88
    3    42    12    35    32     64     106
    4    43    75    64    94     35     169
    5    56    98    35    78     27     176
    6    73    79    95    68     54     174
    7    23    68    56    35     32     124