Skip to content

Instantly share code, notes, and snippets.

@tchakravarty
Created January 23, 2015 18:10
Show Gist options
  • Save tchakravarty/6645d7c05262372690c2 to your computer and use it in GitHub Desktop.
Save tchakravarty/6645d7c05262372690c2 to your computer and use it in GitHub Desktop.
Aggregation with column names specified via regular expressions
dtX = data.table(idx1 = rep(1:3, 100),
idx2 = sample(1:4, size = 10, replace = TRUE, prob = rep(0.25, 4)),
v1 = rnorm(10),
v2 = rnorm(10),
v3 = rnorm(10),
x1 = LETTERS[1:10])
dtXAgg = dtX[, j = lapply(.SD, mean),
by = list(idx1, idx2),
.SDcols = grep("^v", names(dtX), value = TRUE)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment