Mike Hilt
2012-May-31 22:30 UTC
[R] Question on meaning of '%+%', '%?%' (? = various single letter) in code
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 %+% x1MH (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
peter dalgaard
2012-Jun-01 07:32 UTC
[R] Question on meaning of '%+%', '%?%' (? = various single letter) in code
On Jun 1, 2012, at 00:30 , Mike Hilt wrote:> > 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?They are just a vehicle for defining binary operators: functions of two variables but called using infix notation. a %foo% b is equivalent to `%foo%`(a,b) just like 2 + 2 is> `+`(2,2)[1] 4 Like any other function, they do whatever you define them to do, but some are predefined, notably %*% (matrix multiply), %/%, %% (integer divide and remainder), and some packages add their own operators like %+%. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Seemingly Similar Threads
- Can't read xlsx file into R. Seem, Seem to have XLConnect loaded.
- sysdata.rda, namespaces and package dependencies
- rnorm using vector of means and SDs
- Associating the day of week to a daily xts object
- deliver looking into root directory for sieve configuration ?