I have a data frame like this:
D <- data.frame(V1=c(1,2,3,4), V2=c(6,7,8,9), V3=c(3,4,5,6), sign=c("+", "-", "-", "+"))
If the character in column "sign" is "-", I would like to switch values in columns V1 and V2
My desired output would be:
V1 V2 V3 sign
1 1 6 3 +
2 7 2 4 -
3 8 3 5 -
4 4 9 6 +
I have tried for loops and if and else statements, but it is not working. Could you please tell me how I could achieve this?