Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in R Programming by (7.3k points)

I have loaded in an R console different type of objects. I can remove them all using

rm(list=ls())

or remove only the functions (but not the variables) using

rm(list=lsf.str())

My question is: is there a way to remove all variables except the functions

1 Answer

0 votes
by

In this case, you can use the setdiff function to remove all variables except functions as follows:

rm(list = setdiff(ls(), lsf.str())) 

ls() returns the set of objects in the global environment.

lsf.str() returns the set of objects having mode function.

And,

setdiff() is used to find the subset of objects in the global environment not having mode as function.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...