How to delete selected R variables without entering their names

While testing a simulation in R using random generated input, I discovered and fixed several errors and now I would like to re-run the simulation with the same data, but with all the intermediate variables removed to provide a clean test.

Is there a way to remove several dozens of manually selected variables from the workspace without the need to: a) hide the entire workspace, for example. rm(list=ls()) or b) enter each variable name, e.g. remove(name1, name2, ...) ?

The ideal solution is to use ls() to check the definitions and then select the indices of the ones I want to delete, for example

 ls() # inspect definitions delme <- c(3,5,7:9,11,13) # names selected for removal remove(ls()[delme]) # DESIRED SOLUTION -- doesn't quite work this way 

(Looking back, I had to use a fixed seed to generate random input data that would clear everything and then run the test again ...)

+4
variables r reset clear
Feb 10 '14 at
source share
4 answers

Assad, although I think the actual answer to the question is in the comments, let me suggest this template as a broader solution:

 rm(list= Filter( Negate(is.na), # filter entries corresponding to objects that don't meet function criteria sapply( ls(pattern="^a"), # only objects that start with "a" function(x) if(is.matrix(get(x))) x else NA # return names of matrix objects ) ) ) 

In this case, I delete the entire matrix object starting with "a". sapply modifying the pattern argument and the function used by sapply here, you can get fine control over what you delete without specifying a lot of names.

If you are concerned that this might delete something that you do not want to delete, you can save the result of the Filter(... operation Filter(... in a variable, view the contents and then run the rm(list=...) command.

+4
Feb 10 '14 at 13:23
source share

There is a much simpler and more direct solution:

 vars.to.remove <- ls() vars.to.remove <- temp[c(1,2,14:15)] rm(list = vars.to.remove) 

Or, even better, if you are well versed in variable naming schemes , you can use the following pattern matching strategy:

eg. I call all temporary variables the source string "Temp" .... so you can have Temp.Names, Temp.Values, Temp.Whatever

The following is a list of variables matching this pattern.

 ls(pattern = "Temp.") 

This way you can remove all unnecessary variables with the ONE line of code, as shown below:

 rm(list = ls(pattern = "Temp.")) 

Hope this helps.

+1
Jun 30 '17 at 9:38 on
source share

Try

Eval (parsing (text = paste ("RM (", paste (Ls () [Delm], September = ""), ")")))

0
Feb 10 '14 at 13:20
source share

I had a similar requirement. I pulled out all the elements that I need for the list:

 varsToPurge = as.list(ls()) 

Then I reassign a few values โ€‹โ€‹that I want to keep with new variable names that will not be in the varsToPurge variable. After that, I scroll through the elements

 for (j in 1:length(varsToPurge)){ rm(list = as.character(varsToPurge[j])) } 

Do some garbage collection and you maintain a clean environment when you go through your code.

 gc() 

You can also use the vector of line numbers you want to keep and skip the vector in a loop, but it will not be so dynamic if you add the rough work you want to delete.

0
Feb 20 '16 at 23:12
source share



All Articles