R: comparison (6) is possible only for atomic and list types
The code works and leads to no error. The result is a definition of one variable rand one function r_hat.

You get the same with this snippet (I removed the loop as not relevant, what you do is creating the same function repeatedly in a loop)

&gt; r&lt;- matrix(c(2,4, 1,5,5,3, 4,5,1,2,3,4), nrow=3)
&gt; 
&gt; r_hat&lt;- function(u,x) {
+       if (r_hat &gt;r[u,x]) {
+         print(r[u,x]-r_hat)
+       }
+ } 


The problem is in the function, after calling it you get the mentioned error.

&gt; r_hat(0,0)   
Error in r_hat &gt; r[u, x] : 
  comparison (6) is possible only for atomic and list types
&gt; 


You reference r_hat within the function, are you trying to define a recursive function? Than the recursive call should have the parameters and the function some return value...

My guess is you tries to compare the function definition with an element of a matrix, which leads to this error.

If this is not enough for you and you explains what the code should deliver, I'll try to complete my answer.