I have a data.table with a class column and a number of value columns, e.g.
class v1 v2 v3
1: 1 10 3 8
2: 2 2 24 7
3: 1 70 3 9
Now, for a subset of data.table (say class=1), I need to change the order of values in each
row according to a permutation that I have. For instance, if the permutation is
3 1 2
The result should look like
class v1 v2 v3
1: 1 8 10 3
2: 2 2 24 7
3: 1 9 70 3
What’s the best way to achieve this using data.table?
I can alternatively convert my data to matrix, if that’s more efficient. Thanks!
Something like this should work
It would probably be even more efficient to do something like
or
You could also use
:=. something likeYou could try some way of automating this within a function, but it would be a mess of eval / parse and do.call
From Matthew (tested in v1.8.3) :