0

Similar to question here Get the min of two columns

ID    Parm1   Parm2
 1      1       2
 2      0       1
 3      2       1
 4      1       0
 5      2       0

However, Desired output for Min is a non-zero minimum value

 ID    Parm1    Parm2     Min
 1      1        2        1
 2      0        1        1
 3      2        1        1
 4      1        0        1
 5      2        0        2
lsangha
  • 39
  • 4

1 Answers1

1
df %>%
  mutate(across(everything(), ~na_if(.x,0)),
         Min = pmin(Parm1, Parm2, na.rm = TRUE))

  ID Parm1 Parm2 Min
1  1     1     2   1
2  2    NA     1   1
3  3     2     1   1
4  4     1    NA   1
5  5     2    NA   2
Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • Can I get another column with the column name from which that min values is derived. e,g, 5th column would be ``` Param1 , Parm2, Parm2,Parm1,Parm1``` – lsangha Nov 15 '21 at 22:52