How do I clear only a few specific objects from the workspace?
RR Problem Overview
I would like to remove some data from the workspace. I know the "Clear All" button will remove all data. However, I would like to remove just certain data.
For example, I have these data frames in the data section:
data
data_1
data_2
data_3
I would like to remove data_1
, data_2
and data_3
, while keeping data
.
I tried data_1 <- data_2 <- data_3 <- NULL
, which does remove the data (I think), but still keeps it in the workspace area, so it is not fully what I would like to do.
R Solutions
Solution 1 - R
You'll find the answer by typing ?rm
rm(data_1, data_2, data_3)
Solution 2 - R
A useful way to remove a whole set of named-alike objects:
rm(list = ls()[grep("^tmp", ls())])
thereby removing all objects whose name begins with the string "tmp".
Edit: Following Gsee's comment, making use of the pattern
argument:
rm(list = ls(pattern = "^tmp"))
Edit: Answering Rafael comment, one way to retain only a subset of objects is to name the data you want to retain with a specific pattern. For example if you wanted to remove all objects whose name do not start with paper
you would issue the following command:
rm(list = grep("^paper", ls(), value = TRUE, invert = TRUE))
Solution 3 - R
Following command will do
rm(list=ls(all=TRUE))
Solution 4 - R
Use the following command
remove(list=c("data_1", "data_2", "data_3"))
Solution 5 - R
-
In RStudio, ensure the
Environment
tab is inGrid
(notList
) mode. -
Tick the object(s) you want to remove from the environment.
-
Click the broom icon.
Solution 6 - R
You can use the apropos
function which is used to find the objects using partial name.
rm(list = apropos("data_"))
Solution 7 - R
If you just want to remove one of a group of variables, then you can create a list and keep just the variable you need. The rm function can be used to remove all the variables apart from "data". Here is the script:
0->data
1->data_1
2->data_2
3->data_3
#check variables in workspace
ls()
rm(list=setdiff(ls(), "data"))
#check remaining variables in workspace after deletion
ls()
#note: if you just use rm(list) then R will attempt to remove the "list" variable.
list=setdiff(ls(), "data")
rm(list)
ls()
Solution 8 - R
paste0("data_",seq(1,3,1))
# makes multiple data.frame names with sequential number
rm(list=paste0("data_",seq(1,3,1))
# above code removes data_1~data_3
Solution 9 - R
If you're using RStudio, please consider never using the rm(list = ls())
approach!* Instead, you should build your workflow around frequently employing the Ctrl
+Shift
+F10
shortcut to restart your R session. This is the fastest way to both nuke the current set of user-defined variables AND to clear loaded packages, devices, etc. The reproducibility of your work will increase markedly by adopting this habit.
See this excellent thread on Rstudio community for (h/t @kierisi) for a more thorough discussion (the main gist is captured by what I've stated already).
I must admit my own first few years of R coding featured script after script starting with the rm
"trick" -- I'm writing this answer as advice to anyone else who may be starting out their R careers.
*of course there are legitimate uses for this -- much like attach
-- but beginning users will be much better served (IMO) crossing that bridge at a later date.
Solution 10 - R
To clear all data:
click on Misc>Remove all objects
.
Your good to go.
To clear the console:
click on edit>Clear console
.
No need for any code.
Solution 11 - R
You can also use tidyverse
# to remove specific objects(s)
rm(list = ls() %>% str_subset("xxx"))
# or to keep specific object(s)
rm(list = setdiff(ls(), ls() %>% str_subset("xxx")))
Solution 12 - R
Adding one more way, using ls() and remove()
ls() return a vector of character strings giving the names of the objects in the specified environment.
Create a list of objects you want to remove from the environment using ls() and then use remove() to remove it.
remove(list = ls()[ls() != "data"])
Solution 13 - R
Maybe this can help as well
remove(list = c(ls()[!ls() %in% c("what", "to", "keep", "here")] ) )