sweep(A, MARGIN, STATS, FUN="-", ...)
In the most common cases, FUN is "-" or "/" to subtract or divide by statistics that result from using the apply function on the array. For example: colmean <- apply(z,2,mean) computes column means of array z. zcenter <- sweep(z,2,colmean) removes the column means.
More generally, based on MARGIN, there are one or more values of A that would be used by apply to create STATS. sweep creates an array like A where the corresponding value of STATS is used in place of each value of A that would have been used to create STATS. The function FUN is then used to operate element-by-element on each value in A and in the constructed array.
a <- sweep(a,2,apply(a,2,mean)) # subtract col means a <- sweep(a,1,apply(a,1,mean)) # subtract row means # a simple two-way analysis