What is the data.table
way of sorting values within each row? I can easily write a loop which does the sorting row by row, but I suppose it's not a very smart way of doing things.
Example:
Let's have a data.table
like:
df = data.table(ID = c('a', 'b', 'c', 'd', 'e', 'f'),
v1 = c(1,2,1,3,4,5),
v2 = c(2,3,6,1,0,2),
v3 = c(0,0,1,2,3,5))
I can sort this using a for loop
like so:
for (i in 1:nrow(df))
{
df[i, 2:4] = sort(df[i, 2:4], decreasing = T)
}
And it gives the intended result of:
ID v1 v2 v3
1: a 2 1 0
2: b 3 2 0
3: c 6 1 1
4: d 3 2 1
5: e 4 3 0
6: f 5 5 2
But it seems to be very slow way of doing things.