.EACHI in data.table?

I’ve added this to the list here. And hopefully we’ll be able to deliver as planned. The reason is most likely that by=.EACHI is a recent feature (since 1.9.4), but what it does isn’t. Let me explain with an example. Suppose we have two data.tables X and Y: X = data.table(x = c(1,1,1,2,2,5,6), y = … Read more

Summarizing multiple columns with data.table

You can use a simple lapply statement with .SD dt[, lapply(.SD, sum, na.rm=TRUE), by=category ] category index a b z c d 1: c 19 51.13289 48.49994 42.50884 9.535588 11.53253 2: b 9 17.34860 20.35022 10.32514 11.764105 10.53127 3: a 27 25.91616 31.12624 0.00000 29.197343 31.71285 If you only want to summarize over certain columns, … Read more

How to group data.table by multiple columns?

Use by=list(adShown,url) instead of by=c(“adShown”,”url”) Example: set.seed(007) DF <- data.frame(X=1:20, Y=sample(c(0,1), 20, TRUE), Z=sample(0:5, 20, TRUE)) library(data.table) DT <- data.table(DF) DT[, Mean:=mean(X), by=list(Y, Z)] X Y Z Mean 1: 1 1 3 1.000000 2: 2 0 1 9.333333 3: 3 0 5 7.400000 4: 4 0 5 7.400000 5: 5 0 5 7.400000 6: 6 … Read more

How to create a lag variable within each group?

You could do this within data.table library(data.table) data[, lag.value:=c(NA, value[-.N]), by=groups] data # time groups value lag.value #1: 1 a 0.02779005 NA #2: 2 a 0.88029938 0.02779005 #3: 3 a -1.69514201 0.88029938 #4: 1 b -1.27560288 NA #5: 2 b -0.65976434 -1.27560288 #6: 3 b -1.37804943 -0.65976434 #7: 4 b 0.12041778 -1.37804943 For multiple columns: … Read more

Using data.table package inside my own package

Andrie’s guess is right, +1. There is a FAQ on it (see vignette(“datatable-faq”)), as well as a new vignette on importing data.table: FAQ 6.9: I have created a package that depends on data.table. How do I ensure my package is data.table-aware so that inheritance from data.frame works? Either i) include data.table in the Depends: field … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)