How to remove all objects except one or few in R?

R ProgrammingServer Side ProgrammingProgramming

We can use rm remove all or few objects.

Example

< x>-rnorm(100,0.5)
< y>-1:100
< z>-rpois(100,5)
< a>-rep(1:5,20)

To remove all objects

> rm(list=ls())
ls()
character(0)

To remove all except a

> rm(list=setdiff(ls(), "a"))
> ls()
[1] "a"

To remove all except x and a

> rm(list=ls()[! ls() %in% c("x","a")])
ls()
[1] "a" "x"
raja
Updated on 06-Jul-2020 14:42:16

Advertisements