简体   繁体   English

R错误:应用于非向量的names()

[英]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. 我应该在我的代码中指定我没有 - 至少显式 - 一个names()函数。

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. 我能够找到原因,似乎R有一个与缓存函数同名的对象。 This is most likely if using an IDE such as RStudio a tab for View(df). 如果使用诸如RStudio之类的IDE(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. 它的原因是与我很久以前保存的名为“df”的文件有关,这个文件干扰了当前变量,也称为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. 我猜R在类型不匹配时会抛出此错误。 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. 当R再次遇到相同的名称时,它会寻找一个函数,但却找到了一个数据框。

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()) . 每当我尝试通过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 . 在我的情况下,问题是,我有一个名为df (datatype = data.frame)的对象,并且还打开了以前版本的dfView After closing the View the error disappeared. 关闭View ,错误消失了。

Just add 只需添加

 as.vector(dataframe)

This works. 这有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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