Think of apply as a function that "applies" any given operation to the
rows or columns of a matrix. (It also works with higher dimensional
arrays.) To use it, just remember that 1 = 'operate on columns' and
2= 'operate on rows'. (Or remember that 1 leaves the first dimension intact.)
So, if you have an m by n matrix called wanda,
> apply(wanda,1,sum)
will produce a vector of length m (the first dimension is preserved)
whose elements are the row sums from wanda.
Suppose you save the row sums from the wanda matrix in a vector called
fish
> fish <- apply(wanda,1,sum)
The operation
> flatfish <- sweep(wanda,1,fish, "/")
works on each row of wanda (1 = 'operate by rows'). It takes wanda[1,]
and divides (because of the "/") it by fish[1], and so on. The vector
flatfish has length m with wanda[i,]/fish[i] as its ith element.