[R] Question on meaning of '%+%', '%?%' (? = various single letter) in code

Mike Hilt mikehilt21 at gmail.com
Fri Jun 1 00:30:32 CEST 2012


Hello.  I’ve seen several uses of '%?%' in R code (with ? = a single
letter or other characters used as the middle character in a 3
character string with '%' as the 1st and 3rd characters).

I’ve also recently seen ‘%+%’ usage at:

http://vita.had.co.nz/papers/ggplot2-wires.pdf

on p. 7 of the document the following code appears:

(Note the '%+%' string at end of 1st line.)

ggplot(threept, aes(x, y)) %+%
lineup(permute("result"), n = 8) +
geom_point(aes(colour = result, group = 1), alpha = 1/4) +
scale_colour_manual(values = c("made" = mnsl("10B 6/6"),
"missed" = mnsl("10R 6/6"))) +
xlab(NULL) + ylab(NULL) + coord_equal() +
facet_wrap(˜ .sample, nrow = 2)


I've looked long and hard on internet and on the CRAN and can't
find a reference to what '%+%' or '%?%' or ‘%?%’ (? = any letter)
does in an R function/script.

I've also asked several friends who have significant R experience and
all of us are stumped by what '%+%', or more generally '%?%' does in an
R function/script.

xxxxxxxxxxxxx
On p. 12 of:
R Language Definitions
appears:

‘%x% Special binary operators, x can be replaced by any valid name’

and

‘R deals with entire vectors of data at a time, and most of the
elementary operators and basic mathematical functions like log are
vectorized (as indicated in the table above).  This means that e.g.
adding two vectors of the same length will create a vector containing
the element-wise sums, implicitly looping over the vector index.  This
applies also to other operators like -, *, and / as well as to higher
dimensional structures.  Notice in particular that multiplying two
matrices does not produce the usual matrix product (the %*% operator
exists for that purpose).  Some finer points relating to vectorized
operations will be discussed in Section 3.3 [Elementary arithmetic
operations], page 16.’

Page 16 doesn’t specifically address ‘%?%’, at least in a way I can
understand.

xxxxxxxxxxx
I tried an experiment with %+%, and it failed to work!

I loaded xts package (must have xts and zoo installed b/c xts has a
dependency on zoo.)

Entered the following:

> library(xts)
> data(sample_matrix)
> x1 <- sample_matrix
> x1
> x3 <- x1 %/% x1
> x3
> x4 <- x1 %+% x1

MH (Mike Hilt comment):
After entering ‘x3’, the data was listed with all values = 1,
which is what should happen when binary division is done – CHECK!

MH:
After entering ‘x4 <- x1 %+% x1

R returns:
‘Error: could not find function "%+%"’

MH:
So %+% is not a Special binary operator as %x% is
savdescribed on p. 12 of the CRAN doc:
R Language Definitions.

xxxxxxxxxxxx

Could someone help me out and let me know what ‘%?%’
(where ? = a single letter in a 3 character string with ‘%’
being the 1st and 3rd characters), and/or ‘%+%’ does in
R code/function?


Thank you,
Mike Hilt
mikehilt21 at gmail.com



More information about the R-help mailing list