Let's say I need to create empty date tables with the following names (create if it doesn't exist in my environment):
# names of datatables that should be created
dt_list <- c("results_1",
"results_2",
"final_results",
"model_results")
That is, I need to get empty (no columns) data tables: results_1
, results_2
, final_results
, model_results
(in reality, I have a much longer list of date labels that should be created if they don't exist).
I read the thread but didn't find a suitable solution.
I tried something like this, but it doesn't work:
# create an empty data.table if not exists
for(dt in 1:length(dt_list)){
if(!exists(dt_list[dt])){
dt_list[dt] <- data.table()
}
}
Error in dt_list[dt] <- data.table() : replacement has length zero
I would be grateful for any help!
CodePudding user response:
Try this:
# create an empty data.table if not exists
for(dt in 1:length(dt_list)){
if(!exists(dt_list[dt])){
assign(dt_list[dt], data.table())
}
}