0

I have defined multiple variables on the global environment following the rule :

for (i in 1:999){
    assign(sprintf('Model%03d', i), something_i)
}

i.e., I generated variables Model001, Model002, ..., Model999.

But now I should remove those variables from the global environment.


What I tried

I tried the following

for (i in 1:999){
  rm(get(sprintf("Model%03d", i)))
}

But I got the following error

Error in rm(get(sprintf("Model%03d", i))) : 
  ... must contain names or character strings

Alternatively, I tried

for (i in 1:999){
  rm(sprintf("Model%03d", i))
}

but I have the same error message.

I would like to know a shortcut to do this task, even it is quite "dangerous" from the developer's point of view.

Any help will be appreciated.

moreblue
  • 322
  • 1
  • 4
  • 16

0 Answers0