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

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



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"
Updated on: 2020-07-06T14:42:16+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements