aaa<-function() {
for (i in 1:2) {
rm(paste("aaa",i,sep=""),pos = ".GlobalEnv")
}
}
aaa1<-"e!f!g!h!"
aaa2<-"e!f!g!h!"
aaa()
I tried to remove aaa1 and aaa2 with loop, but it doesn't seem to work.
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.