Shuffle the order of a vector around using natural language statements.
moveMe(invec, movecommand)
invec | The input vector |
---|---|
movecommand | The command that describes how you want to shuffle the vector. See Details. |
A vector.
This can be a useful function for reordering the columns of a
data.frame
or data.table
in a convenient manner. In such
cases, the invec
would be names(your_data_frame)
. When using
data.table
s, remember to use setcolorder
to avoid copying.
The movecommand
argument is specified in the form of "a, b
before f"
. The positions to move are:
first: move the specified items to the first postion.
last: move the specified items to the last position.
before: move the specified items before the value mentioned.
after: move the specified items after the value mentioned.
Multiples are allowed:
Specify multiple values to be moved by separating them with a comma.
Chain multiple move commands by separating them with a semicolon.
http://stackoverflow.com/a/18420673/1270695
Ananda Mahto
myvec <- letters[1:10] myvec#> [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"moveMe(myvec, "a last; b, e, g before d; c first; h after j")#> [1] "c" "b" "e" "g" "d" "f" "i" "j" "h" "a"#> [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" #> [11] "carb"moveMe(x, "hp first; cyl after drat; vs, am, gear before mpg; wt last")#> [1] "hp" "vs" "am" "gear" "mpg" "disp" "drat" "cyl" "qsec" "carb" #> [11] "wt"