'names' attribute must be the same length as the vector - 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?

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

I have seen such error and i solved it. You may have missing values in your data set. Number of observations in every column must also be the same.

I want to explain the error with an example below:
> names(lenses)
[1] "X1..1..1..1..1..3"
names(lenses)=c("ID","Age","Sight","Astigmatism","Tear","Class")
Error in names(lenses) = c("ID", "Age", "Sight", "Astigmatism", "Tear", :
'names' attribute [6] must be the same length as the vector [1]
The error happened because of mismatch in a number of attributes. I only have one but trying to add 6 names. In this case, the error happens. See below the correct one:::::>>>>
> names(lenses)=c("ID")
> names(lenses)
[1] "ID"
Now there was no error.
I hope this will help!

I had this, caused by a scaled numeric variable not being returned as numeric, but as a matrix. Restore any transformed variables to as.numeric() and it should work.

The mistake I made that coerced this error was attempting to rename a column in a loop that I was no longer selecting in my SQL. This could also be caused by trying to do the same thing in a column that you were planning to select. Make sure the column that you are trying to change actually exists.

For me, this error was because I had some of my data titles were two names, I merged them in one name and all went well.

I encountered the same error for a silly reason, which I think was this:
Working in R Studio, if you try to assign a new object to an existing name, and you currently have an object with the existing name open with View(), it throws this error.
Close the object 'View' panel, and then it works.

Related

R Studio: Creating a empty list results in an Error: 'names' attribute [1] must be the same length as the vector [0]

I am quite new to R and tried to create an empty list (names Input_IMPLEMENTATION) using:
Input_IMPLEMENTATION<-vector(mode="list",length=7)
However, this resulted in the following error:
Error in names(x) <- paste("V", seq_along(x), sep = "") :
'names' attribute [1] must be the same length as the vector [0]
If I use a different name for the list, everything works fine. for example:
Input_IMPLEMENTATION_a<-vector(mode="list",length=7)
Therefore, I assumed the error occurs as Input_IMPLEMENTATION is allready assigned some value. So I tried rm(Input_IMPLEMENTATION) before executing the code, but this did not change anything.
Does anyone have a hint for me, why I get this error?

R: error in dimnames when using colnames with matrix

Imagine the case to preallocate a 2x2 matrix with NAs. Now I would like to rename the first column to "Test" with
name_matrix<-matrix(NA,2,2)
colnames(name_matrix)[1] <- "Test"
But now I am getting the error message:
Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent
Whereas if I convert the matrix to a data.frame first I am not ending up with this error message.
name_data_frame<-as.data.frame(name_matrix)
colnames(name_data_frame)[1] <- "Test"
My question is does anybody have an idea how to find out more about this unexpected behavior and maybe as well how to fix it?
The problem is that after the first line
name_matrix<-matrix(NA,2,2)
the value of colnames(name_matrix) is NULL for which it does not make sense to access its first entry colnames(name_matrix)[1].
Instead you have to assign the entire variable a vector of length 2.
For instance you could do:
colnames(name_matrix)<- c("Test", NA)

What is the difference between c(1:4) and a <- c(1:4) while passing it to a dataframe to retrieve a set of columns

I have a dataframe with 10 columns in it. I want to retrieve the columns 1:4.
However I am storing the value 1:4 in a variable. Let's call it a. When I try to use df[,c(a)] it just gives me the numbers back. However, if I give df[,c(1:4)] it gives me the columns. What is the difference? If I use the function identical on these two: identical(c(a),c(1:4)), it gives me TRUE. What is that I am doing wrong?
I was able to figure this out. It was giving me this error :
Error in [.data.table(tr, 1, a) :
j (the 2nd argument inside [...]) is a single symbol but column name 'a' is not found. Perhaps you intended DT[,..a] or DT[,a,with=FALSE]. This difference to data.frame is deliberate and explained in FAQ 1.1.
So I had to use with=FALSE. This solved it.
Thanks!

R language - Unrecognized Error relating rbind

Has anyone seen this error before? -
Error in colnames<-(tmp, value = 1:59) :
'names' attribute [59] must be the same length as the vector [0]
If yes, how can I fix with it?
This error would occur, if you try to assign column names to an empty data.frame:
d <- data.frame()
colnames(d) <- 1:59

lapply fail, but function works fine for each individual input arguments

Many thanks in advance for any advices or hints.
I'm working with data frames. The simplified coding is as follows:
`
f<-funtion(name){
x<-tapply(name$a,list(name$b,name$c),sum)
1) y<-dataset[[deparse(substitute(name))]]
#where dataset is an already existed list object with names the same as the
#function argument. I would like to avoid inputting two arguments.
z<-vector("list",n) #where n is also defined already
2) for (i in 1:n){z[[i]]<-x[y[[i]],i]}
...
}
lapply(list_names,f)
`
The warning message is:
In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
and the output is incorrect. I tried debugging and found the conflict may lie in line 1) and 2). However, when I try f(name) it is perfectly fine and the output is correct. I guess the problem is in lapply and I searched for a while but could not get to the point. Any ideas? Many thanks!
The structure of the data
Thanks Joran. Checking again I found the problem might not lie in what I had described. I produce the full code as follows and you can copy-paste to see the error.
n<-4
name1<-data.frame(a=rep(0.1,20),b=rep(1:10,each=2),c=rep(1:n,each=5),
d=rep(c("a1","a2","a3","a4","a5","a6","a7","a8","a9","a91"),each=2))
name2<-data.frame(a=rep(0.2,20),b=rep(1:10,each=2),c=rep(1:n,each=5),
d=rep(c("a1","a2","a3","a4","a5","a6","a7","a8","a9","a91"),each=2))
name3<-data.frame(a=rep(0.3,20),b=rep(1:10,each=2),c=rep(1:n,each=5),
d=rep(c("a1","a2","a3","a4","a5","a6","a7","a8","a9","a91"),each=2))
#d is the name for the observations. d corresponds to b.
dataset<-vector("list",3)
names(dataset)<-c("name1","name2","name3")
dataset[[1]]<-list(c(1,2),c(1,2,3,4),c(1,2,3,4,5,10),c(4,5,8))
dataset[[2]]<-list(c(1,2,3,5),c(1,2),c(1,2,10),c(2,3,4,5,8,10))
dataset[[3]]<-list(c(3,5,8,10),c(1,2,5,7),c(1,2,3,4,5),c(2,3,4,6,9))
f<-function(name){
x<-tapply(name$a,list(name$b,name$c),sum)
rownames(x)<-sort(unique(name$d)) #the row names for
y<-dataset[[deparse(substitute(name))]]
z<-vector("list",n)
for (i in 1:n){
z[[i]]<-x[y[[i]],i]}
nn<-length(unique(unlist(sapply(z,names)))) # the number of names appeared
names_<-sort(unique(unlist(sapply(z,names)))) # the names appeared add to the matrix
# below
m<-matrix(,nrow=nn,ncol=n);rownames(m)<-names_
index<-vector("list",n)
for (i in 1:n){
index[[i]]<-match(names(z[[i]]),names_)
m[index[[i]],i]<-z[[i]]
}
return(m)
}
list_names<-vector("list",3)
list_names[[1]]<-name1;list_names[[2]]<-name2;list_names[[3]]<-name3
names(list_names)<-c("name1","name2","name3")
lapply(list_names,f)
f(name1)
the lapply(list_names,f) would fail, but f(name1) will produce exactly the matrix I want. Thanks again.
Why it doesn't work
The issue is the calling stack doesn't look the same in both cases. In lapply, it looks like
[[1]]
lapply(list_names, f) # lapply(X = list_names, FUN = f)
[[2]]
FUN(X[[1L]], ...)
In the expression being evaluated, f is called FUN and its argument name is called X[[1L]].
When you call f directly, the stack is simply
[[1]]
f(name1) # f(name = name1)
Usually this doesn't matter, but with substitute it does because substitute cares about the name of the function argument, not its value. When you get to
y<-dataset[[deparse(substitute(name))]]
inside lapply it's looking for the element in dataset named X[[1L]], and there isn't one, so y is bound to NULL.
A way to get it to work
The simplest way to deal with this is probably to just have f operate on character strings and pass names(list_names) to lapply. This can be accomplished fairly easily by changing the beginning of f to
f<-function(name){
passed.name <- name
name <- list_names[[name]]
x<-tapply(name$a,list(name$b,name$c),sum)
rownames(x)<-sort(unique(name$d)) #the row names for
y<-dataset[[passed.name]]
# the rest of f...
and changing lapply(list_names, f) to lapply(names(list_names),f). This should give you what you want with nearly minimal modification, but you also might consider also renaming some of your variables so the word name isn't used for so many different things--the function names, the argument of f, and all the various variables containing name.

Resources