1

I know when I want to remove all files, I simply perform

rm(list=ls(gse))

if I want to remove one file for example named "data" I can do

rm(data) 

what if I have so many files and I want to remove everything but only keep one or two or few that I want ?

2 Answers2

6

For example

rm(list = ls()[!ls() %in% c("abc", "def")])

should remove everything but abc and def.

lukeA
  • 53,097
  • 5
  • 97
  • 100
0

You could, as an alternative to lukeA's nice answer, write the few files of interest to a savethese.Rdata file, then rm(list=ls(gse)) followed by re-loading the savethese.Rdata file. Klugey, but with the advantage of backing up the items you want.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73