Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'names' attribute must be the same length as the vector

Tags:

r

Stuck on an error in R.

    Error in names(x) <- value : 
      'names' attribute must be the same length as the vector

What does this error mean?

like image 471
Sheila Avatar asked May 12 '12 18:05

Sheila


2 Answers

In the spirit of @Chris W, just try to replicate the exact error you are getting. An example would have helped but maybe you're doing:

  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]

I suspect you're trying to give names to a vector (x) that is shorter than your vector of names (y).

like image 111
user1317221_G Avatar answered Oct 20 '22 00:10

user1317221_G


Depending on what you're doing in the loop, the fact that the %in% operator returns a vector might be an issue; consider a simple example:

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

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

then the following warning is issued:

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

if something in your if statement is dependent on a specific number of elements, and they don't match, then it is possible to obtain the error you see

like image 3
ChrisW Avatar answered Oct 20 '22 01:10

ChrisW