. Advertisement .
..3..
. Advertisement .
..4..
I am new to r and searching the “dim(x) must have a positive length” to understand it better. It seems it doesn’t work as expected when I used some suggestions before. Here is the command line I use:
best_recom <- function(x,n=1) {
y <- result2[x,order(-result2[x,])[n]]
inds = which(result2[x,] == y, arr.ind=TRUE)
recom <- names(inds[1])
return(recom)
}
apply(last_visit[,2], 1, best_recom)
dim(X) must have a positive length
apply(as.matrix(last_visit)[,2],1,recomenda_n_melhor)
X1.0 X1.1 X2.1 X3.1
X1.0 0.0000000 0.5000000 0.3872983 0.3162278
X1.1 0.5000000 0.0000000 0.2581989 0.0000000
X2.1 0.3872983 0.2581989 0.0000000 0.0000000
X3.1 0.3162278 0.0000000 0.0000000 0.0000000
customer cat
1 1 X5.1
2 2 X6.1
3 3 X1.1
4 4 X2.1
The error I’m getting is below:
dim(X) must have a positive length
Please give me the solution to this issue.
The cause: The error appears because R coerces
last_visit[,2]
to a dimensionless vector, whereasapply
needs to have some dimensions of the object.Solution: You can stop the compulsion by adding
drop=F
to the command:Or on the vector, you use
lapply
orsapply
like this: