A B
2 3
5 7
7 4
2 8
Is there any way to swap specific observations? I am trying to swap the observations in A that are > than the observations in the same row of B. In the data.frame above the observation in the third row of column A (7) is > the observation in the third row of column B (4) my goal is to swap the two.
Desired output
A B
2 3
5 7
4 7
2 8
I have tried using an &
df$A[df$A > df$B] <- df$B[df$A > df$B] & df$B[df$A >df$B] <- df$A[df$A >df$B]
I have also tried a nested ifelse
ifelse(df$A > df$B, df$A[df$A > df$B] <- df$B[df$A > df$B],
ifelse(df$A > df$B, df$B[df$A >df$B] <- df$A[df$A >df$B], df$b))
Any help would be very much appreciated.
Dre