dim(X) must have a positive length when applying function in data frame

It happens because R coerces last_visit[,2] to a dimensionless vector, whereas apply expects the object to have some dimensions. You can prevent the coercion by adding drop=F to your command, i.e.:

apply(last_visit[,2,drop=F], 1, best_recom)

Another way would be just to use lapply or sapply on the vector:

lapply(last_visit[,2], best_recom)

Leave a Comment