Reputation: 20415
I have an array of object names v <- c("v1", "v2")
, which associates with objects v1
, v2
.
How should I remove these objects using rm()
?
I tried rm(mget(v))
, but I got error:
Error in rm(mget(v)) : ... must contain names or character strings
Upvotes: 1
Views: 8310
Reputation: 2651
--Editing the answer based on better understanding of the problem as pointed out by Dason--
Since the intention is remove the objects referred to by the contents of v & not v itself, rm(v)
(as was suggested earlier by me) is inappropriate (as it will remove v though not the objects pointed to by the contents of v (viz. v1 & v2).
> v1 <- "A"
> v2 <- "B"
> #v is a vector containing the references to v1 & v2 as character strings
> v <- c("v1","v2")
> v
[1] "v1" "v2"
> rm(v)
> v
Error: object 'v' not found
> v1
[1] "A"
> v2
[1] "B"
>
The OP wishes to remove the objects v1 & v2 & not v. As Adam identified, the solution would be rm(list=v)
> rm(list=v)
> v
[1] "v1" "v2"
> v1
Error: object 'v1' not found
> v2
Error: object 'v2' not found
>
Upvotes: 9