在一对列中找到最大值/最小值 [英] Find the max/min value in a pair of columns

查看:71
本文介绍了在一对列中找到最大值/最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据如下:

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)

如何选择 A 之间的最大值/最小值和 B

How might I select the max/min value between A and 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


推荐答案

您可以尝试 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 为您提供两列的最大值个单一值,而不是行最大值

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

这篇关于在一对列中找到最大值/最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆