Search code examples
rswap

Is it possible to swap columns around in a data frame using R?


I have three variables in a data frame and would like to swap the 4 columns around from

"dam"   "piglet"   "fdate"   "ssire"

to

"piglet"   "ssire"   "dam"   "tdate"

Is there any way I can do the swapping using R?

Any help would be very much appreciated.

Baz


Solution

  • dfrm <- dfrm[c("piglet", "ssire", "dam", "tdate")]
    

    OR:

    dfrm <- dfrm[ , c("piglet", "ssire", "dam", "tdate")]