How to Delete Variables in R?

Spread the love

Problem –

You want to remove unneeded variables or functions from your workspace or to erase its contents completely.

Solution –

To delete or remove a variables from your workspace, you can use the rm function. The rm function removes a variable permanently from the workspace.

Let’s create some variables.

> a <- 5
> b <- 10
> c <- a + b
> f <- function(n, p) sqrt(p * (1-p) / n)

Now, let’s say you want to remove the a variable. You can do it like this

> rm(a)

You can also remove several variables at once.

> rm(b, c, f)

You can even erase your entire workspace at once. The rm function has a list argument consisting of a vector of names of variables to remove. Previously we saw that ls function returns a vector of variable names. So we can combine rm and ls to erase everything.

> ls()
[1] "a" "b" "c" "f"
> rm(list = ls())
> ls()
character(0)

To remove the variables you can also use the broom icon at the top of the environment pane in RStudio.

Rating: 1 out of 5.

Posted in RTagged

Leave a Reply