-1

I am creating a series of data.tables via a loop where each tables name and columns are dependent on the loop.

I found I can use assign to create the table and then use eval(as.name(tbl)) to then call it which seems to work ok. The column name doesn't seem to be behaving quite like I want though?

To generate the column name I use capture.output(str(tbl, give.head = F)) which works but then when I want to reference the column it is surrounded by double quotes e.g. "name_win_pcnt"

I can't seem to reference the column either so if I use name_win_pcnt$"name_win_pcnt" I get a NULL in the console.

Here is an example.

require(data.table)
# initial data table
dt <- data.table(x = rnorm(10),
             y = rnorm(10),
             grp = c(rep("a",3), rep("b",7))))

#variables
metric <- c("win", "place")
cols <- "name"

tbl <- paste0(cols, "_", metric[1],"_pcnt")

# create new table and create new column
assign(tbl, dt, envir = .GlobalEnv)
eval(as.name(tbl))[, capture.output(str(tbl, give.head = F)) := 0L, by = .(grp)]

If I now try and update the new column using

eval(as.name(tbl))[, eval(tbl) := 1L, by = .(grp)] this creates me a new column but leaves the old one?

I tried adding the column using eval(as.name(tbl))[, eval(tbl) := 0L] but then when I try and update it I get an error:

Error in is.nan(name_win_pcnt) : 
  default method not implemented for type 'list'
3
  • 2
    Do not use assign. Put all these data.tables in a list.
    – Roland
    Commented May 31, 2017 at 8:58
  • I create the tables 1 at a time using my outer loop. Why should I avoid assign? Commented May 31, 2017 at 9:15
  • 1
    Because of the kind of problems you are encountering ... assign is for experts who know when it is needed (almost never). Using a list (or environment) is the "R way".
    – Roland
    Commented May 31, 2017 at 10:06

1 Answer 1

2

I think that the usage of the set command would be more in the spirit of the data.table package and can do the job.

 set(x=eval(as.name(tbl)), j=tbl, value=2L)

This way, no quotation marks are in the column name.

Althought you didn't ask, I feel that using lists to hold all data.tables together would make better use of R's data structures.

4
  • apologies I should have mentioned I use I need to perform operations on groups so I don't think set will work in this case. I will update my question. Commented May 31, 2017 at 9:45
  • maybe you can do the subset operation before and use set in a second step on an intermediate result? In a third step you could update the data in the original data.table again with set.
    – mondano
    Commented May 31, 2017 at 9:49
  • Thanks doing the intermediate step seems to have worked. So I create the column using my original method, then use set to update it and it seems to cure the problem. It seems using variables is quite hard work in R? Commented May 31, 2017 at 10:01
  • It is unusual and I don't do it very often, but it is feasible, the language can deal with it. If your case works with set, please mark my answer as accepted. Thanks!
    – mondano
    Commented May 31, 2017 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.