Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R remove variable after assign() using paste0()

Tags:

r

I need to create a variable using assign() to give it a dynamic name. I later need to remove the variable.

day_no <- 1
df <- data.frame( a = 1:3, b = 4:6 )
assign( paste0( 'newdf_' ,day_no ), df )

This works fine.

The trouble is removing the variable afterwards. I have tried:

rm( paste0( 'newdf_', day_no ) )

Or:

rm( parse( paste0( 'newdf_', day_no ) ) )

However I get this error message:

Error in rm(parse(paste0("newdf_", day_no))) : 
... must contain names or character strings

I have searched a lot online but cannot find the answer, some help would be much appreciated.

like image 939
user3740289 Avatar asked Sep 19 '25 14:09

user3740289


1 Answers

For non-interactive use, use the list argument to pass your character name(s).

rm(list = paste0("newdf_", day_no))
like image 159
Rich Scriven Avatar answered Sep 22 '25 05:09

Rich Scriven