Olivia
Olivia

Reputation: 814

R - making objects null

I had a large data set to create and did so in pieces. I created 50 data frames (F1:F50) and then I merged them together with:

for(i in 1:50) {x= get(paste0("F",i))
zF <- rbind.fill(zF,x)}

However now I'm trying to clear the other objects.

Once they are NULL I use

rm(list=names(which(sapply(globalenv(),is.null))))

Perhaps I could change is.null() to something like a pattern that describes my objects, but I thought it would be easier to just look through and make each NULL so I tried a few attempts:

> for(i in 1:56) {get(paste0("F",i))
     paste("F",i)=NULL}
Error in paste("F", i) = NULL : 
  target of assignment expands to non-language object

> for(i in 1:56){get(paste0("F",i))=NULL}
    Error in get(paste0("F", i)) = NULL : 
      target of assignment expands to non-language object

So where the top loop worked but these failed implies to me that whilst get will give you the data to replicate (in the form of x) and then use, it does not allow me to change the actual object.

Is there a way to do this or is the solution using rm, I'd prefer the former as it opens up future changes to objects other than simply getting rid of them.

Upvotes: 1

Views: 186

Answers (1)

GGamba
GGamba

Reputation: 13680

rm(list = ls(pattern = '^F'))

Remove all objects that match the pattern (in this case all objects that start with 'F')

Upvotes: 2

Related Questions