Reputation: 175
I have two columns,say, a & b, and I have to swap the values of a & b if the value in column b is greater than the value in column a.
I've written a function to swap the variable if the condition is met
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)
The output should come as :
a b
110 70
120 80
110 80
But I'm getting an error
the condition has length > 1 and only the first element will be used
Upvotes: 2
Views: 1443
Reputation: 3195
Gregor's solution is the best, but thought I'd add an extra.
swap_if <- function (a, b, missing = NA) {
c <- a
x <- ifelse(b > a, b, a)
y <- ifelse(b <= a, b, c)
z <- data.frame(x, y)
return(z) }
swap_if(a, b)
x y
1 110 70
2 120 80
3 110 80
Upvotes: 2
Reputation: 145755
If it's only 2 columns, pmin
and pmax
are your friends here.
temp_min = pmin(df$a, df$b)
df$a = pmax(df$a, df$b)
df$b = temp_min
If it's more than 2 columns, than the apply sort scales better.
Upvotes: 6
Reputation: 101034
Assuming your input is given as
df <- data.frame(a = c(110,120,80), b = c(70,80,110))
Then the following can give the expected output:
df_swap <- as.data.frame(t(apply(df, 1, function(v) sort(v,decreasing = T))))
colnames(df_swap) <- names(df)
such that
> df_swap
a b
1 110 70
2 120 80
3 110 80
Upvotes: 2