R: error in dimnames when using colnames with matrix - r

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)

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?

Correlating multiple columns in a matrix to a single column in the same matrix

I have a matrix of 48 variables with 40 observation each. I am trying to correlate the first 47 columns separately to the 48th column. what I've tried to do is use cor command:
cor(x[,1:47], x[,48], method="kendall").
I'm getting an error:
Error in cor.test.default(Hj1[, 1:47], Hj1[48], method = "kendall") :
'x' and 'y' must have the same length
Since each column is the same length, I understand it's not about the column lengths, but something else. what can it be?
Thanks!
David.
Look at your error message:
Error in cor.test.default(Hj1[, 1:47], Hj1[48], method = "kendall") :
'x' and 'y' must have the same length
Hj1[48].
That was a typo. You wanted Hj1[,48]
Ok, I've separated the column from the matrix and run the correlation successfully.
In my original code it looks like this:
Hj1tox <- Hj1[,48]
Hj1_ab <- Hj1[,1:47]
cor(Hj1_ab, Hj1tox)

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

Using a geocode script given and getting errors in R

I have to perform a geocode script in R. I have a script given and I have tried to get it to work, but keep getting error. I am pasting my code below and the errors I am getting. If you could guide me in the correct direction, I would appreciate it.
# initialise a dataframe to hold the results
geocoded <- data.frame()
# find out where to start in the address list (if the script was interrupted before):
startindex <- 1
# if a temp file exists - load it up and count the rows!
tempfilename <- paste0(hw1, '_temp_geocoded.rds')
if (file.exists(tempfilename)){
print("Found temp file - resuming from index:")
geocoded <- readRDS(tempfilename)
startindex <- nrow(geocoded)
print(startindex)
}
## Warning message:
## In if (file.exists(tempfilename)) { :
## the condition has length > 1 and only the first element will be used
The function file.exists(...) returns a logical vector of length equal to the length of its argument. So if you provide a vector of file names, file.exists(...) will return a vector of the same length, where each element is either TRUE or FALSE depending on whether the corresponding file exists.
The problem is that if(...) expects a scalar (vector of length 1). Otherwise, it just uses the first element of the vector and gives the warning you are seeing.
So, I suspect that hw1 is a vector of length > 1, which will make tempfilename a vector of length > 1.

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

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.

Resources