Three Diag
Three Diag

Reputation: 585

Normalizing by column: the data.table way

Starting with a simple datatable

ID MAX X1 X2 X3 X4
E0 9   2  1  2  3
E0 21  9  0  10 2

I would like to normalize all columns from X1 to X4 dividing by MAX.

The unelegant, data.frame way would be

dt$X1 <- dt$X1/dt$MAX

What is the elegant data table way?

I tried with dt[, .SD/MAX, by = ID], but this also reduces MAX to 1.

Upvotes: 3

Views: 1462

Answers (1)

akrun
akrun

Reputation: 887028

Using data.table v1.9.7, we can also specify the columns in .SDcols, loop through the columns (lapply(.SD, ...), divide by the 'MAX' and assign (:=) the output back to the columns of interest.

dt[, paste0("X", 1:4) := lapply(.SD, `/`, MAX), .SDcols= X1:X4]
dt
#   ID MAX        X1        X2        X3        X4
#1: E0   9 0.2222222 0.1111111 0.2222222 0.3333333
#2: E0  21 0.4285714 0.0000000 0.4761905 0.0952381

Upvotes: 6

Related Questions