3

I know we can dynamically add column names when creating columns by reference (using :=), as described e.g. here: Dynamic column names in data.table.

However, I'm looking to dynamically add column names when we aggregate. Can you help with this?

test_dtb <- data.table(a = sample(1:100, 100), b = sample(1:100, 100), id = rep(1:10, 10))
m = "blah"
test_dtb[ , list((m) = mean(b)), by = id]

The error I get is

Error: unexpected '=' in "test_dtb[ , list((m) =
Henrik
  • 65,555
  • 14
  • 143
  • 159
ArunK
  • 1,731
  • 16
  • 35
  • 1
    [Maybe this helps](http://stackoverflow.com/a/37143023/2204410) – Jaap Dec 20 '16 at 09:55
  • I'm programmatically creating the m variable and its length can vary from 1 to 5. I think it would be hard to define which index to setnames from. – ArunK Dec 20 '16 at 10:03
  • 3
    What about `m = c("blah", "foo");test_dtb[,setNames(list(mean(b),median(b)), m),by = id]`? – lukeA Dec 20 '16 at 10:24
  • `test_dtb[,eval(m):=mean(b),by = id]` ? – Tensibai Dec 20 '16 at 17:23
  • works with multiple values also: `m = c("blah", "foo"); test_dtb[,eval(m):=list(mean(b),median(b)),by = id,verbose=TRUE]` gives: `a b id blah foo` columns – Tensibai Dec 20 '16 at 17:29

2 Answers2

5

As mentioned in the comments by lukeA, setNames can be used:

m <- c("blah", "foo")
test_dtb[ , setNames(list(mean(b), median(b)), m), by = id] 
Henrik
  • 65,555
  • 14
  • 143
  • 159
ArunK
  • 1,731
  • 16
  • 35
-2

Just make sure data.table knows "m" is not a variable name.

Parentheses or "c" will do.

test_dtb[, (m)  := mean(b),by = id]
test_dtb[, c(m) := mean(b),by = id]
Community
  • 1
  • 1
rflores
  • 1
  • 1
  • 1
    As I mentioned in the question, its while I'm aggregating. The answers in the comments help with that. – ArunK Dec 21 '16 at 08:36
  • 1
    Woops! My mistake. In this case, the solution suggested by lukeA is an excellent one. – rflores Dec 21 '16 at 22:23