hora
hora

Reputation: 855

Remove a variable implicitly in R

Suppose I have set of variables test1, test2, test3, ..., testn. I wanted to remove them in a for loop using "eval" but it does not work. What is the solution? Thank you in advance.

for (i in 1:5)
rm(eval(parse(text=(paste0("test",i)))))

Error in rm(eval(parse(text = (paste0("test", i))))) : 
  ... must contain names or character strings
Error during wrapup: cannot open the connection

Upvotes: 2

Views: 241

Answers (1)

zx8754
zx8754

Reputation: 56149

As many mentioned no need to loop

Arun's solution:

rm(list=grep("^test[0-9]+$", ls(), value=TRUE))

If you insist using loop then:

for (i in 1:5)
  rm(list=ls()[ls()==paste0("test",i)])

Upvotes: 1

Related Questions