Skip to content

'x' and 'w' must have same length - error in weighted.mean.default

An answer to this question on Stack Overflow.

Question

I am having a problem with the package glmnet in R. I am trying to use it off-the-shelf, and am getting the following problem:

test <- glmnet(seq.trans,rsem.trans)

Error in weighted.mean.default(y, weights) : 'x' and 'w' must have the same length

But the inputs are the same size:

dim(seq.trans)
# [1]    28 17763
dim(rsem.trans)
# [1]    28 17763

What is causing this error?

Answer

In the context of glmnet(x,y) the variable y should be a vector.

In your example, you could achieve this using:

glmnet(seq.trans, as.vector(rsem.trans))