I have a data.table that looks like this:
ID1 | ID2 | ID3 | ID4 | subtotal | total |
---|---|---|---|---|---|
001 | 001 | 001 | 001 | 10 | 100 |
001 | 001 | 001 | 002 | 5 | 20 |
001 | 002 | 001 | 001 | 10 | 200 |
Using shiny I can then select which ID's I want to group by, for example ID1 till ID3:
ID1 | ID2 | ID3 | subtotal | total |
---|---|---|---|---|
001 | 001 | 001 | 15 | 120 |
001 | 002 | 001 | 10 | 200 |
As you can see the first row of this table is a sum of the first two rows of the first table.
I then calculate the percentage, the column will automatically be put at the very end:
ID1 | ID2 | ID3 | subtotal | total | percentage |
---|---|---|---|---|---|
001 | 001 | 001 | 15 | 120 | 12.5 |
001 | 002 | 001 | 10 | 200 | 5 |
However, I would like see this column just after the IDs.
I tried to use setcolorder
however the columns can vary depending on which IDs are selected. The IDs that are used are stored in a vector which I tried to use like so:
dt[, .(vector, percentage, subtotal, total)]
and:
dt[, c(vector, "percentage", "subtotal", "total")]
but neither option worked
for reference (keep in mind that it should work for any combination of IDs):
dput(vector)
c("ID1", "ID2", "ID3")