R Error: names() applied to a non-vector

Manfredo picture Manfredo · Sep 21, 2016 · Viewed 18.5k times · Source

I have a chunk of code which produces an error only the first time I run it. Strangely if I run it a second time I get no error (craziness definition?). Also the error does not show up always at the same position, I mean that if I add a few lines of comments the error message is printed after the comments and not after a specific instruction.

I cannot provide a reproducible example because I do not know where exactly the error comes from. The error is the following:

Error in names(frame)[names(frame) == "x"] <- name : 
  names() applied to a non-vector

I should specify that in my code I don't have -at least explicitly- a names() function.

Answer

This is a tricky error. I was able to track down the reason and it seems to be that R has an object of the same name as the function cached. This is most likely if using an IDE such as RStudio a tab for View(df). Unless the tab is closed even running the function without code will give you this error. Likewise, if the tab is not closed not even removing all objects or doing a garbage collection will solve it. Once the tab is closed the error will be gone.