I'm trying to do a row apply in data.table
and can't get it to work. How do I do so?
library(data.table)
data(diamonds, package= "ggplot2")
dt <- data.table(diamonds)
# what I want, but via data.table
diamonds$sum1 <- apply(dt[,5:10, with=FALSE], 1, sum)
diamonds$sd1 <- apply(dt[,5:10, with=FALSE], 1, sd)
# why don't these work?
dt[, `:=` (sum1= sum(.SD),
sd1= sd(.SD)), .SDcols= 5:10, by= .EACHI]
dt[, `:=` (sum1= sum(dt[,5:10, with=FALSE]),
sd1= sd(dt[,5:10, with=FALSE])), by= .EACHI]
Both give this error:
Error in
[.data.table
(dt, ,:=
(sum1 = sum(.SD), sd1 = sd(.SD)), .SDcols = 5:10, : object 'f__' not found