Search code examples
rvectormultiple-columnsswap

How to swap the values in two columns in R?


V1    V2
 5     6
 4     4
 2     5

How would I swap the values in these two column vectors. So that the outcome would be:

V1    V2
 6     5
 4     4
 5     2

Solution

  • the easiest way to do this:

    tmp <- df[1]
    df[1] <- df[2]
    df[2] <- tmp