Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove selected R variables without having to type their names

Tags:

variables

r

reset

While testing a simulation in R using randomly generated input data, I have found and fixed a few bugs and would now like to re-run the simulation with the same data, but with all intermediate variables removed to ensure it's a clean test.

Is there a way to remove several dozen manually selected variables from the workspace without having to: a) clobber the entire workspace, e.g. rm(list=ls()), or b) type each variable name, e.g. remove(name1, name2, ...)?

Ideal solution would be to use ls() to inspect the definitions and then pick out the indices of the ones I want to remove, e.g.

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

(In hindsight, I should have used a fixed seed to generate the random input data, which allow clearing everything and then re-running the test...)

like image 483
Assad Ebrahim Avatar asked Feb 10 '14 12:02

Assad Ebrahim


People also ask

How do I remove certain variables in R?

Using rm() command: When you want to clear a single variable from the R environment you can use the “rm()” command followed by the variable you want to remove. variable: that variable name you want to remove.

How do I remove a value in R?

rm() function in R Language is used to delete objects from the memory. It can be used with ls() function to delete all objects. remove() function is also similar to rm() function.

How do I remove an object from a workspace in R?

Actually, there are two different functions that can be used for clearing specific data objects from the R workspace: rm() and remove(). However, these two functions are exactly the same. You can use the function you prefer. The previous R code also clears the data object x from the R workspace.


2 Answers

Assad, while I think the actual answer to the question is in the comments, let me suggest this pattern 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'm removing all matrix object that start with "a". By modifying the pattern argument and the function used by sapply here, you can get pretty fine control over what you delete, without having to specify many names.

If you are concerned that this could delete something you don't want to delete, you can store the result of the Filter(... operation in a variable, review the contents, and then execute the rm(list=...) command.

like image 117
BrodieG Avatar answered Oct 21 '22 15:10

BrodieG


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, better yet, if you are good about variable naming schemes, you can use the following pattern matching strategy:

E.g. I name all temporary variables with the starting string "Temp." ... so, you can have Temp.Names, Temp.Values, Temp.Whatever

The following produces the list of variables that match this pattern

ls(pattern = "^Temp\\.")

So, you can remove all unneeded variables using ONE line of code, as follows:

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

Hope this helps.

like image 6
HoneyBuddha Avatar answered Oct 21 '22 17:10

HoneyBuddha