R provides powerful functions. Apply function is one of them. It keeps you from writing boring loops(while and for). For insance:

# create a matrix of 10 rows x 2 columnsm <- matrix(c(1:10, 11:20), nrow = 10, ncol = 2)

# divide all values by apply

apply(m, 1:2, function(x) x/2)

Thanks for nsaunders