I'm working with a HUGE data.frame in R. My dataset follows the pattern:
+-------+------+------+------+------+ | | Col1 | Col2 | Col3 | Col4 | +-------+------+------+------+------+ | Line1 | 43 | a | b | 56 | | Line2 | 103 | c | d | 85 | | Line3 | 7 | F | E | 115 | | Line4 | 8 | g | h | 0 | +-------+------+------+------+------+
I have to do the following:
For each row:
If Col2 > Col3 (check alphabetical order of the values)
Swap values of Col 2 and Col 3
I need the following result:
+-------+------+------+------+------+ | | Col1 | Col2 | Col3 | Col4 | +-------+------+------+------+------+ | Line1 | 43 | a | b | 56 | | Line2 | 103 | c | d | 85 | | Line3 | 7 | E | F | 115 | | Line4 | 8 | g | h | 0 | +-------+------+------+------+------+
I wrote a for loop for this, but it takes a LONG LONG time! Is there a more efficient way to do this with R?
Thank you!