I have a data.table similar to this one:
require(data.table)
dt = as.data.table(iris[ names(iris) != 'Species' ])[1:26]
dt[ , id := LETTERS ]
setnames(dt, c('col1', 'col2', 'col3', 'col4', 'id'))
I want to calculate the absolute difference of col1 to every other numeric columns (i.e. col2, col3, col4) for every id/row. How can I do that with data.table or other tools in R?
The result should look like this:
+--------+-------------+-------------+-------------+------+
| "col1" | "col1_col2" | "col1_col3" | "col1_col4" | "id" |
+--------+-------------+-------------+-------------+------+
| 5.1 | 1.6 | 3.7 | 4.9 | "A" |
| 4.9 | 1.9 | 3.5 | 4.7 | "B" |
+--------+-------------+-------------+-------------+------+