简体   繁体   中英

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

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.

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.

I had the same error, and It was faulting on a particular function that I wrote. It was throwing the error whenever I loaded the function, even when the I commented out all the code in the function. I found that changing the variable name of my function stopped the error. My only guess is that there is some weird variable name conflict.

I had the same error. The reason for it is something related to a file I saved long ago with the name "df", which is interfering with a current variable, also called df.

The solution however is straigthforward: Find the problematic line, by running the first line of the code, then clearing the global environment, and running it again. If no error occurs, add the next line, and so forth until the error occurs. Then, change the name of the variable in that line.

Even I had the same error. The reason in my case was that there was another data frame with the same name as the function inside the function. I guess R throws this error when there is a type mismatch as well. In my case the name was first read as a function. When R came across the same name again, it would be looking for a function but instead found a data frame.

All I had to do was change the name of one of them and the error was gone.

This error also appears in my code everytime when I tried to delete everything through remove(list = ls()) . In my case the problem was, that I had an object named df (datatype = data.frame) and also had the View opened of a previous version of df . After closing the View the error disappeared.

Just add

 as.vector(dataframe)

This works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM