0

I have a syntax question because I do not understand the behaveior of data.table for my problem. Similiar to this question I want to paste two columns directly together using a predefined character vector. I do not want to create a new column. MWE:

dt <- data.table(L=1:5,A=letters[7:11],B=letters[12:16])
cols<-c("A", "B")

I can paste directly using the col names without brackets as from the other question

dt[,paste0(A,B)]

But i cant using with=F or .SD

dt[,paste0(cols),with=F]
dt[,paste0(.SD),.SDcols=cols]

Why do I have to use a do.call?

dt[,do.call(paste0,.SD), .SDcols=cols]
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Max M
  • 806
  • 14
  • 29
  • What's wrong with `do.call`? It should be very efficient. – David Arenburg Nov 09 '17 at 10:57
  • 3
    Reiterating David's point. It seems to accomplish what you need - what's wrong with `do.call`? If you're asking _why_ `do.call` works, recall that `.SD` (and your `data.table` `dt`) is just a "spruced-up"/regularized `list`. The syntax for `do.call` is `do.call(some_function, list_of_arguments)`. – MichaelChirico Nov 09 '17 at 10:59
  • Nothing wrong with it. But I do not understand the need to use it. I would have intuitively used with=F or .SDcols. I only by accident saw the do.call way in another slighly related post – Max M Nov 09 '17 at 11:01
  • 3
    imo, this is not a data.table issue but due to the implementation of `paste` – talat Nov 09 '17 at 11:04
  • 3
    `do.call(f, args)` is R's (oddly named) idiom for passing a list of args, just like `f(*args)`/`f(**kwargs)` is the syntax in python. – Frank Nov 09 '17 at 13:04
  • Thanks for the effort, To be honest I still do not understand why I Need this to do what I want. I was hoping to understand this. – Max M Nov 09 '17 at 16:18

0 Answers0