Reputation: 187
When trying to clear out an R workspace, why does code snippet #1 work, but not #2
Snippet #1
rm(list = ls())
Snippet #2
list = ls()
rm(list)
Upvotes: 3
Views: 2151
Reputation: 226182
Because you are not naming the argument (i.e. the list=
part of the command), R interprets list
as an object to be removed, not a list of arguments to be removed: from ?rm
:
rm (..., list = character(), pos = -1, envir = as.environment(pos), inherits = FALSE)
Arguments
...
the objects to be removed, as names (unquoted) or character strings (quoted)
list
a character vector naming objects to be removed.
(This would be true even if you called the variable something else, e.g. junk = ls(); rm(junk)
vs. rm(list=junk)
)
Upvotes: 5