195

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.

0

13 Answers 13

279

You'll find the answer by typing ?rm

rm(data_1, data_2, data_3)
4
  • 48
    For deleting all variables you can use rm(list=ls()).
    – larkee
    Oct 21, 2014 at 22:23
  • 2
    but how can you do with the GUI, without the command line? like in Revolution R
    – skan
    Sep 13, 2015 at 11:42
  • 2
    what if I want to remove all variables with names start with letter A, say 'A001', 'A002'.... 'A999'. I don't want to type so many variable names. Thanks! Sep 22, 2015 at 16:04
  • 4
    Use @mbask's answer (just below): rm(list = ls()[grep("A", ls())])
    – Pop
    Sep 24, 2015 at 7:51
93

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))
5
  • 8
    ls accepts a pattern argument, so you can simplify that to rm(list=ls(pattern="^tmp"))
    – GSee
    Aug 25, 2012 at 13:52
  • 1
    Separately, check out what happens when you use value=TRUE with grep... could save some typing.
    – GSee
    Aug 25, 2012 at 13:57
  • Hi, is there a way to keep some few data and remove the others (a larger amount)? Cheers
    – Rafael
    Jul 6, 2013 at 13:56
  • 1
    Hi! And how could I keep in my R environment two different patterns? I tried "rm(list = grep("^paper" | "ObjectExample", ls(), value = TRUE, invert = TRUE))". Does anyone know?
    – Dekike
    Nov 1, 2019 at 10:11
  • 1
    You should include your regular expression into the pattern argument of grep: rm(list = grep("^paper|ObjectExample", ls(), value = TRUE, invert = TRUE))
    – mbask
    Nov 4, 2019 at 8:00
15

Following command will do

rm(list=ls(all=TRUE))
2
  • 3
    This will remove everything from the workspace, not just data1, data2, and data3 (i.e. data will be removed too). Which is fine if that's what you want...
    – Phil
    Jun 4, 2015 at 9:57
  • 2
    And what if one wants to remove only data, not functions, values or other objects in, say, the global environment? Aug 2, 2015 at 23:35
15
  1. In RStudio, ensure the Environment tab is in Grid (not List) mode.

  2. Tick the object(s) you want to remove from the environment.

  3. Click the broom icon.

1
  • its popup should read 'Clear selected objects...'
    – ivan866
    May 14, 2022 at 22:50
10

You can use the apropos function which is used to find the objects using partial name.

rm(list = apropos("data_"))
0
10

Use the following command

remove(list=c("data_1", "data_2", "data_3"))
4

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()
3
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
2

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.

2

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"])
1

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.

1
  • 1
    Though I had to do Session> Clear Workspace....
    – Peter K.
    Jan 28, 2016 at 15:36
1

Maybe this can help as well

remove(list = c(ls()[!ls() %in% c("what", "to", "keep", "here")] ) )
0

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")))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.