OPEN ARCHIVE · 2016

Apply Function in R: Apply same function to for each cell of a matrix

· By · 1 min read

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