0

New here so apologies in advance!

I am using data.tables in R and I want to 'combine' various columns (say, V1, V2, V3,...). I have the names of the columns in a list (for example, lst <- c("V1", "V2")). So, basically I want to create a new column V3 such as

DT[, V3: = combine(lst)]

Also, the idea is that lst can be of any length and I am concerned about efficiency.

I don't mind what kind of format the union has (char, numeric, etc.). Any ideas?

Thank you very much in advance.

TitoOrt
  • 1,265
  • 1
  • 11
  • 13
  • 3
    Welcome to StackOverflow. Please take a look at these tips on how to produce a [minimum, complete, and verifiable example](http://stackoverflow.com/help/mcve), as well as this post on [creating a great example in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Perhaps the following tips on [asking a good question](http://stackoverflow.com/help/how-to-ask) may also be worth a read. – lmo Mar 16 '17 at 13:48
  • 1
    Your use of the term "merge" does not match the r function of that name. Type `?merge` to see more. You'll probably also want to read through the data.table vignettes (though maybe after looking at some r tutorials). – Frank Mar 16 '17 at 14:14
  • I used merge for a lack of a better word. I roughly know how data.table works, and I know how to for example paste different columns together. The problem is that I usually need to type the name of each column manually and what I am trying to do is to just using a list of column names. – TitoOrt Mar 16 '17 at 14:22
  • Then you probably need something like `do.call(paste, .SD), .SDcols = lst` – talat Mar 16 '17 at 14:25
  • 1
    by "combine" do you mean sum, concatenate, create an average, do some kind of NVL style function? As mentioned above providing examples and examples of desired result are going to get you more quality answers. – ike Mar 16 '17 at 14:27
  • This: Then you probably need something like do.call(paste, .SD), .SDcols = lst – It worked like a charm! Thanks! – TitoOrt Mar 16 '17 at 14:34

0 Answers0