5

My data looks like this:

df <- tribble(
    ~A, ~B,     
    0.2, 0.1,
    0.2, 0.3,
    0.5, 0.1,
    0.7, 0.9,
    0.8, 0.9,
    0.4, 0.2)

How might I select the max/min value between A and B?

Desired Output:

   A    B    C  
1  0.2  0.1  0.2
2  0.2  0.3  0.3
3  0.5  0.1  0.5
4  0.7  0.9  0.9
5  0.8  0.9  0.9
6  0.4  0.2  0.4
emehex
  • 9,874
  • 10
  • 54
  • 100

1 Answers1

15

You could try pmax

mutate(df, C=pmax(A,B))
#      A   B   C
#1 0.2 0.1 0.2
#2 0.2 0.3 0.3
#3 0.5 0.1 0.5
#4 0.7 0.9 0.9
#5 0.8 0.9 0.9
#6 0.4 0.2 0.4

max gets you the maximum single value of the two columns instead of the "row" maximum

akrun
  • 874,273
  • 37
  • 540
  • 662