I have a data table, and I'm trying to create a new variable, which is a function of all the other columns. A simplified example would be if I just wanted to sum or take the average of all the lines. For instance:
dt <- data.table(a = 1:9, b = seq(10,90,10), c = seq(11:19), d = seq(100, 900, 100))
I want to create a vector / column that is just the average value for all columns. A syntax that I think would look something like this:
dt[, average := mean(.SD)]
However, this sums it all up. I also know what I can do:
dt[, average := lapply(.SD, mean)]
But this gives a single line result. I am basically looking for the equivalent:
dt[, average := lapply(.SD, mean), by = all]
so he just calculates this for all rows, without creating an id column and doing all my calculations on that column. Is it possible?
source share