R function to generate predictions from ratings
Without exact your data I could think of some improvements.
Trying to avoid redundant operations.

# order data.frame by users and movies
probe_test &lt;- probe_test[with(probe_test, order(user, move)), ]
# initialize resulting column
probe_test$res &lt;- rep(as.numeric(NA), nrow(probe_test))

knn_txt_red &lt;- knn_txt[, 1:100] # reduce outside of the loop
for (user in unique(probe_test$user)) { # for each unique user
  u_row &lt;- which(U_lookup == user)[1] # get your id
  knn_match &lt;- knn_txt_red[u_row, ]
  knn_match1 &lt;- as.numeric(unlist(knn_match))
  userI &lt;- probe_test$user == user
  movies &lt;- probe_test$movie[userI] #get all user movies
  m_row &lt;- which(M_lookup %in% movies) # get indexes
  dfm_mov &lt;- dfm[knn_match1, m_row] #select all cols of those movies for user
  x &lt;- colMeans(dfm_mov) # calculate mean for each row
  probe_test[userI, 'res'] &lt;- x # add the results to data.frame
}


As I do not have your data, there are probably/maybe some errors in code.

There are probably better ways to do this, but as I mentioned, it is hard to think of any without any example data.