I won't speak for Jim, as he's more than capable of responding to this
himself, but I'll give it a shot:
(1) It's not just the 'double percent sign', it's that slash in between
them, too. Certain operators in R happen to be enclosed between two percent
%/% is the integer division operator in R; it returns the integer part of
> 10 %/% 2
> 9 %/% 2 # integer part of 4.5
> 11 %/% 2 # integer part of 5.5
%% is the modulo (remainder from integer division) operator in R:
> 10 %% 2
> 11 %% 2
> 12 %% 2
(2) The sapply() function is commonly used to apply a function across a
vector of arguments; e.g., many loops can be replaced with equivalent
sapply() statements. In this case, the sequence used as the first argument
of sapply() consists of the odd numbers up to the number of columns in the
matrix; for each element of that sequence, the function computes the
difference between that odd numbered column in the matrix and the adjacent
even numbered column to its right.
Henrique's solution does the same thing, except he uses a factor variable
(gl(2, 2)) to distinguish odd from even-numbered columns. It's useful to
look at both solutions for a general answer to the problem you posed.
I see that others have already responded, but will add my point of
view. You indicated that you wanted to take the difference between
pairs of columns and did not specify exactly how many there were; in
your example there were 4 columns (2 pairs). If there were only two,
then the solution from Dennis would be the way to go because it is
explicit in what has to be done. I just created a list of the "odd"
columns [seq(from = 1, by = 2, length = ncol(x) %/%2)] trying to take
care of any odd number of columns. This was then used in the 'sapply'
to iterate over the columns.