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?
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
).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With