Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Subtract the row maximum from another column in R

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading