length of 'center' must equal the number of columns of 'x' - r

I'm trying to scale a matrix in R.
I have:
maxs<- apply(rawsingle,2,max)
mins<- apply(rawsingle,2,min)
and then
scaledss<- scale(rawsingle,center=mins,scale = (maxs-mins))
I get the error:
'Error in scale.default(rawsingle, center = mins, scale = (maxs - mins)) :
length of 'center' must equal the number of columns of 'x''
Immediately following the error, I typed:
length(mins)==ncol(rawsingle)
and it returned TRUE, so I have no idea whats going on.
Has anyone had a similar issue before?

All the variables needs to be of type numeric:
dades[2:13] <- lapply(dades[2:13], as.numeric)

The problem was that some elements in 'rawsingle' were character strings due to an error in loading it.

Related

Compute the mean in R subject to conditions

I tried following the advice on this post (Conditional mean statement), however it did not seem to work for me. I get the error Error in x[j] : only 0's may be mixed with negative subscripts.
So I have a database called data with a few different columns and many rows. There is one indicator column, z, which takes value 0 or 1. I want to compute the mean of the column base if z depending on whether z=0 or z=1. So I have used the following line of code:
mean(data[data$z==1, data$base], na.rm = TRUE)
But as mentioned, I get the error Error in x[j] : only 0's may be mixed with negative subscripts. I'm unsure why I am getting this error, or what I could/should do instead. I do not actually understand the error.
Thanks.
Using the comment by GKi worked and solved my problem. In the end I used
mean(data$base[data$z==1], na.rm = TRUE)

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)

The 'height' component of 'tree' is not sorted Error in cutree

I am trying to do some grouping and am encountering this error.
Evaluation error: the 'height' component of 'tree' is not sorted (increasingly).
My input is:
library(stringdist)
name <- c("luke,abcdef","luke,abcdeh","luke,abcdeg")
a<-stringdistmatrix(name, method="jw")
clusts <- hclust(a, method="ward.D2")
But when I try to cut it, it gives me an error:
> cutree(clusts, h = 0.155)
Error in cutree(clusts, h = 0.155) :
the 'height' component of 'tree' is not sorted (increasingly)
But if I use
a<-stringdistmatrix(name, method="jw", p=0.05)
everything works fine.
I have looked for a solution and couldn't find one.
What should I do, to prevent this from happening and keep it working?
I have also noticed, that if I have the same distance matrix, but generated by hand (so there is no distance parameter in the cluster.
If you compare diff(clusts$height) for these two examples, the first comes out as a tiny negative number, the second as exactly zero. So the problem is caused by binary-representation rounding differences in values that should be equal.
It should work if you round the heights after calculating clusts...
clusts$height <- round(clusts$height, 6)

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)

'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