Find the max/min value in a pair of columns

假装没事ソ 提交于 2020-01-04 02:56:11

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/28632617/find-the-max-min-value-in-a-pair-of-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!