我有两列,例如a和b,并且如果b列中的值大于a列中的值,则必须交换a&b的值。
如果条件满足,我已经编写了一个交换变量的函数
a b
110 70
120 80
80 110
swap_if<- function (a,b,missing=NA)
{
if(b>a)
{
a = a + b
b = a - b
a = a - b
}
}
swap_if(a,b)
输出应为:
a b
110 70
120 80
110 80
但是我遇到了错误
the condition has length > 1 and only the first element will be used