Remove multiple objects with rm()
Make the list a character vector (not a vector of names)
rm(list = c('temp1','temp2'))
or
rm(temp1, temp2)
Removing workspace objects whose name start by a pattern using R
The pattern argument uses regular expressions. You can use a caret ^
to match the beginning of the string:
rm(list=ls(pattern="^tp_"))
rm(list=setdiff(ls(pattern = "^tp_"), lsf.str()))
However, there are other patterns for managing temporary items / keeping clean workspaces than name prefixes.
Consider, for example,
temp<-new.env()
temp$x <- 1
temp$y <- 2
with(temp,x+y)
#> 3
rm(temp)
Another possibility is attach(NULL,name="temp")
with assign
.
How to remove objects with rm()
You cannot pass a function that returns a character vector like that to rm
. For example if you run
aaa1<-12
x<-"aaa1"
rm(x)
it will remove x
, not aaa1
. So you can't remove variables by giving a list of strings to rm()
like that.
However, rm()
does have list=
parameter that does allow you to specify a character vector. so
aaa<-function() {
for (i in 1:2) {
rm(list=paste("aaa",i,sep=""),pos = ".GlobalEnv")
}
}
aaa1<-"e!f!g!h!"
aaa2<-"e!f!g!h!"
aaa()
will work.
Remove objects in environment with for loop
You have to make a small modification
for (i in 1:5) {
rm(list = paste("query",i,sep = ""))
}
How can I remove all objects but one from the workspace in R?
Here is a simple construct that will do it, by using setdiff
:
rm(list=setdiff(ls(), "x"))
And a full example. Run this at your own risk - it will remove all variables except x
:
x <- 1
y <- 2
z <- 3
ls()
[1] "x" "y" "z"
rm(list=setdiff(ls(), "x"))
ls()
[1] "x"
rm( ) everything except specific object
The setdiff()
function shows the difference between sets, so we can use this to give the difference between all the objects (ls()
), and the object you want to keep. For example
## create some objects
df <- data.frame()
v <- as.numeric()
# show everything in environment
objects()
# [1] "df" "v"
## or similarly
ls()
# [1] "df" "v"
## the setdiff() funciton shows the difference between two sets
setdiff(ls(), "df")
# [1] "v"
# so we can use this to remove everything except 'df'
rm(list = setdiff(ls(), "df"))
objects()
# [1] "df"
Related Topics
Display a Time Clock in the R Command Line
Logical Operators (And, Or) with Na, True and False
How to Read Only Lines That Fulfil a Condition from a CSV into R
Add "Filename" Column to Table as Multiple Files Are Read and Bound
What's the Best Way to Use R Scripts on the Command Line (Terminal)
Using Gsub to Extract Character String Before White Space in R
Chi Square Analysis Using for Loop in R
Replace Contents of Factor Column in R Dataframe
Gsub() in R Is Not Replacing '.' (Dot)
Find the Intersection of Overlapping Ranges in Two Tables Using Data.Table Function Foverlaps
Examples of the Perils of Globals in R and Stata
Calculate Cumulative Average (Mean)
Find Duplicated Rows (Based on 2 Columns) in Data Frame in R
Merge and Perfectly Align Histogram and Boxplot Using Ggplot2
Insert Picture/Table in R Markdown
Plot 4 Curves in a Single Plot with 3 Y-Axes