Clear All In R: Clear The Environment

clear all in r

In this article, we will discuss how to use clear all in the R programming language. Let’s read it right now.

Why do we clear all in R?

It is sometimes necessary to clear variables when programming. There is a simple mechanism in R for removing R objects from the R environment. You can also remove some or all of your variables. One reason you might want to delete variables is to reduce clutter. You can see the method to clear it below.

How to clear all in R?

Using the rm() function to clear the environment

In this example, we can delete one or more variables using the rm() function in the R language. The simplest straightforward method is using the object’s argument, as seen below.

# Initial data
x <- 12
y <- c(5, 4, 9, 6, 3, 2)
z <- 2:10

# View data
x
y
z

Output:

[1] 12
[1] 5 4 9 6 3 2
[1] 2  3  4  5  6  7  8  9 10

Now, we will clear all by using the rm() function as follows:

# Initial data
x <- 12
y <- c(5, 4, 9, 6, 3, 2)
z <- 2:10

# Clear all
rm(x, y, z)

# View data
x
y
z

Output:

Error: object 'x' not found
Error: object 'y' not found
Error: object 'z' not found

Otherwise, you can use the list argument to clear the environment.

# Initial data
x <- 12
y <- c(5, 4, 9, 6, 3, 2)
z <- 2:10

# Clear all
rm(list = c("x", "y", "z"))

# View data
x
y
z

Output:

Error: object 'x' not found
Error: object 'y' not found
Error: object 'z' not found

Another example is that the rm() command prompt is invoked with the list parameter, which is equivalent to the ls() function, which returns a vector of the names of the items in the global environment or browser cache.

# Initial data
x <- 12
y <- c(5, 4, 9, 6, 3, 2)
z <- 2:10

# Clear all
rm(list = ls())

# View data
x
y
z

Output:

Error: object 'x' not found
Error: object 'y' not found
Error: object 'z' not found

Using the Broom Icon to clear the environment 

Here, we will use the broom icon in the Rstudio to clear the environment as follows:

Another example clears all with the plot. First, we will create a data and plot this data as shown below:

# Create a data
x <- c(2, 6, 3, 1, 4, 5, 2, 7.9)
y <- c(1:8)

# Plot
plot(x, y, col = "red")

Output:

Now, we will use the broom icon in the Rstudio to clear all the graphs which we drew with this data above.

Summary

This article demonstrates how to use clear all in the R programming language with rm() function and broom icon. If you have any questions, please leave a comment below. Good luck for you!

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *