我正在尝试将此函数应用于数据框列:
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)
但我得到同样的错误。
这些是使用的数据框:
result2 - 相似度矩阵 - 这只是一个示例
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