Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all objects from environment except those matching a given pattern

Tags:

r

I'm trying to remove all objects from my RStudio environment where the object names are NOT equal to a pattern.

rm(list=ls(pattern!="may19"))

However this gives me an error

Error in as.environment(pos) : no item called "pattern != "may19"" on the search list

Is there another way to approach this? Thanks in advance

like image 326
Varun Avatar asked Mar 16 '26 16:03

Varun


1 Answers

We could use one of the following(other variants might exist, you can add all=TRUE or all.names=TRUE) for completeness:

rm(list=setdiff(ls(),"may19"))
rm(list=ls(pattern = "[^may19]"))
like image 87
NelsonGon Avatar answered Mar 18 '26 05:03

NelsonGon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!