Table of Contents
The environment in R refers to the set of variables and functions that are currently stored in the memory. It is important to clear the environment at times to avoid any potential errors or conflicts while running a new code. There are various methods to clear the environment in R.
One method is to use the “rm()” function, which allows the user to remove specific variables or objects from the environment. This can be done by specifying the names of the variables or using the wildcard “*” to remove all objects.
Another method is to use the “detach()” function, which removes a specific package from the environment. This is useful when working with multiple packages and wanting to clear the environment of a specific package.
Additionally, the “gc()” function can be used to clear unused memory and reduce the size of the environment. This is particularly helpful when working with large datasets.
One can also use the “restart” button in the RStudio interface to completely clear the environment and restart the R session.
Overall, clearing the environment in R is a crucial step in ensuring smooth and error-free execution of code. It is important to be familiar with these different methods to efficiently manage the environment in R.
Clear the Environment in R (3 Methods)
There are three methods you can use to quickly clear the environment in R:
Method 1: Clear Environment Using rm()
rm(list=ls())Method 2: Clear Environment Using the Broom Icon

Method 3: Clear Specific Types of Objects Using lm() and class
#clear all data frames from environment
rm(list=ls(all=TRUE)[sapply(mget(ls(all=TRUE)), class) == "data.frame"])
#clear all lists from environment
rm(list=ls(all=TRUE)[sapply(mget(ls(all=TRUE)), class) == "list"])
The following examples shows how to use each of these methods in practice.
Method 1: Clear Environment Using rm()
Suppose we have an R environment with two data frames, two lists, two matrices, and two vectors:

We can use the following code to remove all objects from the envinroment:
rm(list=ls())
Notice that every object in the R environment is now cleared.
Method 2: Clear Environment Using the Broom Icon
Once again suppose we have an R environment with the following objects:

We can click the broom icon to clear the entire environment:

Once we click Yes, the environment will be cleared:

Method 3: Clear Specific Types of Objects
Occasionally we may only want to clear specific types of objects from the environment in R.
For example, suppose we have an R environment with the following objects:

We can use the following code to clear only the data frames from the environment:
#clear all data frames from environment
rm(list=ls(all=TRUE)[sapply(mget(ls(all=TRUE)), class) == "data.frame"])
Notice that all of the data frames have been cleared from the environment but all of the other objects remain.
Cite this article
stats writer (2024). How can the environment be cleared in R using various methods?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-environment-be-cleared-in-r-using-various-methods/
stats writer. "How can the environment be cleared in R using various methods?." PSYCHOLOGICAL SCALES, 12 May. 2024, https://scales.arabpsychology.com/stats/how-can-the-environment-be-cleared-in-r-using-various-methods/.
stats writer. "How can the environment be cleared in R using various methods?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-environment-be-cleared-in-r-using-various-methods/.
stats writer (2024) 'How can the environment be cleared in R using various methods?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-environment-be-cleared-in-r-using-various-methods/.
[1] stats writer, "How can the environment be cleared in R using various methods?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can the environment be cleared in R using various methods?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
