Predict new ratings for each user based on their pearson correlation with other users
You have incorrect indexing in the loop. Better version with mapply and correct indexing:

df &lt;- mapply(function(i, j) {
  sim_user = which(cor_mat[i,] &gt;= lower &amp; cor_mat[i,] &lt; upper)
  final_x[j, i] = t(x[j, sim_user]) %*% (cor_mat[j, sim_user]/sum(cor_mat[j, sim_user]))
  cbind(i, j, sim_user, cor_mat[j, sim_user])
}, 1:ncol(x), 1:nrow(x))


This version already 44 times faster than the loop version:

    test replications elapsed relative user.self sys.self user.child sys.child
1   loop          100    2.21     44.2      2.21        0         NA        NA
2 mapply          100    0.05      1.0      0.05        0         NA        NA