Home > other >  Subtract the row maximum from another column in R
Subtract the row maximum from another column in R

Time:08-30

I want to subtract the maximum value in each row by another column, and then make a new column with these values. So if I have this, and I want to subtract the max value from col1:

col1   col2    col3     col4      col5
1       3       2        3          5
3       7       4        1          2
2       2       6        8          3

To get:

col1   col2    col3     col4      col5     new
1       3       2        3          5       -4
9       7       4        1          2        2
2       2       6        8          3       -6

I'm thinking I need to use mutate and then maybe the maxstat package. Any help is appreciated!

CodePudding user response:

We could use pmax to get the max of each row and then subtract

df1$new <- df1$col1 - do.call(pmax, c(df1[-1], na.rm = TRUE))

-output

> df1
  col1 col2 col3 col4 col5 new
1    1    3    2    3    5  -4
2    9    7    4    1    2   2
3    2    2    6    8    3  -6

Or with tidyverse

library(dplyr)
library(purrr)
df1 %>%
   mutate(new = col1 - exec(pmax, !!! rlang::syms(names(.)[-1]),
         na.rm = TRUE))
  col1 col2 col3 col4 col5 new
1    1    3    2    3    5  -4
2    9    7    4    1    2   2
3    2    2    6    8    3  -6

Or we could use

df1 %>% 
  mutate(new = col1 - invoke(pmax, across(-col1), na.rm = TRUE))
  col1 col2 col3 col4 col5 new
1    1    3    2    3    5  -4
2    9    7    4    1    2   2
3    2    2    6    8    3  -6

data

df1 <- structure(list(col1 = c(1, 9, 2), col2 = c(3L, 7L, 2L), col3 = c(2L, 
4L, 6L), col4 = c(3L, 1L, 8L), col5 = c(5L, 2L, 3L)),
 row.names = c(NA, 
-3L), class = "data.frame")
  •  Tags:  
  • r
  • Related