To remove all objects but one from R workspace, use the setdiff function from the sets base R package.
The basic syntax for setdiff is given below:
setdiff(x, y)
x, y :-
Vectors (of the same mode) containing a sequence of items (conceptually) with no duplicate values.
For example:
The code below will remove all the objects in R workspace except “x”
x <- c("A","B","C","D")
y <- 2 z <- data.frame(name = "SAM", age = 22, gender = "Male") rm(list=setdiff(ls(), "x"))
ls()
[1] "x"
If you want to explore more in R programming then watch this R programming tutorial for beginners: