0

I have a problem with data tables in R. I have a vector col_Names which contains the names of some columns of the datatable dataset.

I want to compute the log of the columns in col_Names and to substitute the result to the original ones... If I know the names of these columns and if they are a few, say for example

col_Names = c("col1","col2")

I could use this code:

dataset[,':='(col1=log(col1),col2=log(col2))]

but since I don't know the nature of col_Names because it's created by another procedure I can't use that code.

is there any code, I don't know, like

dataset[,':='(col_Names=log(col_Names))]

which could help me? I don't want to use for cycle or lapply if it's possible...

lmo
  • 37,904
  • 9
  • 56
  • 69
Bmb58
  • 155
  • 2
  • 9
  • 3
    ```dataset[, (col_Names) := lapply(.SD, log), .SDcols = col_Names]``` – David Arenburg Jun 20 '17 at 11:36
  • There should be a duplicate question for this somewhere. – talat Jun 20 '17 at 11:36
  • David Arenburg...I have specified that I want to do it without lapply if it's possible, thank you anyway! – Bmb58 Jun 20 '17 at 11:38
  • Here is a [closely related post](https://stackoverflow.com/questions/13623324/transform-a-set-of-columns-in-a-data-table) that uses `lapply`. @FrancescoTamberi `lapply` or some other looping variant is almost certainly necessary. – lmo Jun 20 '17 at 11:41
  • 1
    `lapply` is the idiomatic way to operate over columns in data.table. What's wrong with it? Running `log` over the whole data set will convert it to matrix first which will be needed to be converted back. Looping over column is much cheaper. Unless you prefer working with matrices instead of data.tables in the first place. – David Arenburg Jun 20 '17 at 11:46
  • 1
    why so much hatred toward lappy? i know it's sometimes inefficient but in this specific situation, it shouldn't be that bad, unless you have thousands of columns. – amatsuo_net Jun 20 '17 at 11:48
  • ahah nono I don't hate lapply :) I know how to use it, I'm searching for something, if it exists, which works better for data table...I don't know how many columns will I have, because it's not in mine project, but I want to know if there's an alternative, regarding datatables, for lapply...only for the taste of learn! – Bmb58 Jun 20 '17 at 11:53
  • 1
    A different option is to use a `for` loop in combination with `set` from data.table – talat Jun 20 '17 at 11:56
  • Your title is misleading. You want to overwrite existing columns, not create new ones... and so the question is essentially the same as https://stackoverflow.com/q/16846380/ (setting aside your antipathy towards `lapply`)..? – Frank Jun 20 '17 at 12:06

0 Answers0