'names'属性必须与向量的长度相同

She*_*ila 15 r

坚持R中的错误

    Error in names(x) <- value : 
      'names' attribute must be the same length as the vector
Run Code Online (Sandbox Code Playgroud)

这个错误是什么意思?

use*_*1_G 15

本着@Chris W的精神,试着复制你得到的确切错误.一个例子会有所帮助,但也许你正在做:

  x <- c(1,2)
  y <- c("a","b","c")
  names(x) <- y

Error in names(x) <- y : 
  'names' attribute [3] must be the same length as the vector [2]
Run Code Online (Sandbox Code Playgroud)

我怀疑你试图给一个x比你的名字(y)向量更短的vector()命名.


Chr*_*isW 3

根据您在循环中执行的操作,%in%运算符返回向量这一事实可能是一个问题;考虑一个简单的例子:

c1 <- c("one","two","three","more","more")
c2 <- c("seven","five","three")

if(c1%in%c2) {
    print("hello")
}
Run Code Online (Sandbox Code Playgroud)

然后发出以下警告:

Warning message:
In if (c1 %in% c2) { :
  the condition has length > 1 and only the first element will be used
Run Code Online (Sandbox Code Playgroud)

如果 if 语句中的某些内容依赖于特定数量的元素,并且它们不匹配,则可能会出现您看到的错误