2012-05-12 20 views
6

這個錯誤是什麼意思?「名稱」屬性必須是相同的長度R.</p> <pre><code>Error in names(x) <- value : 'names' attribute must be the same length as the vector </code></pre> <p>憋屈錯誤矢量

+2

你能提供一個最小可重現的例子嗎? – Dason

+2

是的,我強烈懷疑你已經指出了錯誤的線......所以需要更多的上下文!嘗試在錯誤發生後鍵入'traceback()'。 – Tommy

+1

您是否檢查過此討論:http://stackoverflow.com/questions/14153092/meaning-of-ddply-error-names-attribute-9-must-be-the-same-length-as-the-vec –

回答

0

根據你在循環中做什麼,%in%運算符返回一個向量的事實可能是一個問題;考慮一個簡單的例子:

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

if(c1%in%c2) { 
    print("hello") 
} 

那麼以下警告:

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

如果事情在你的if語句是依賴於要素的具體數量,以及它們不匹配,那麼它是可能獲得你看到的錯誤

9

在@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] 

我懷疑你想給名字向量(x)比你的名字的載體(y)短。

-1

我看到了這樣的錯誤,我解決了它。您的數據集中可能缺少值。每列中的觀察數量也必須相同。

0

我強迫這個錯誤的錯誤是試圖重命名一個循環中的列,我不再選擇在我的SQL中。這也可能是由於您想要在計劃中進行選擇而嘗試做同樣的事情造成的。確保您試圖更改的列實際存在。

相關問題