6

I have been working with R for about 2 months and have had a little bit of trouble getting a hold of how the $ and %% terms.

I understand I can use the $ term to pull a certain value from a function (e.g. t.test(x)$p.value), but I'm not sure if this is a universal definition. I also know it is possible to use this to specify to pull certain data.

I'm also curious about the use of the %% term, in particular, if I am placing a value in between it (e.g. %x%) I am aware of using it as a modulator or remainder e.g. 7 %% 5 returns 2. Perhaps I am being ignorant and this is not real?

Any help or links to literature would be greatly appreciated.

Note: I have been searching for this for a couple hours so excuse me if I couldn't find it!

5
  • I am not entirely educated on all uses of '%%' but I know %in% is used for element inclusion and %*% is matrix multiplication. So maybe it is used for vector and matrix operations? Jul 9, 2015 at 20:14
  • statmethods.net/management/operators.html - wrt 7 %% 5 Jul 9, 2015 at 20:14
  • also %>% + variants are used as pipes (a coding method influenced by FSharp) in the magrittr package. Searching for that should get you started.
    – Chris
    Jul 9, 2015 at 20:20
  • '%in% is defined on the ?match page. Function with flanking %-signs are called "specials". The authors of magrittr and other packages that define "+" methods for graphical objects have defined additional functions which are being dispatched using the R class system which was originally being used for math or logical operations. This is usually called "overloading". The operator precedence is detailed in ?Syntax help page. And the ?Ops page might be interesting reading as well.
    – IRTFM
    Jul 9, 2015 at 20:32
  • The $ doesn't really fit, but the %% and %*% elements of this question are a dupe of this r-faq. Jul 9, 2015 at 20:44

3 Answers 3

13

You are not really pulling a value from a function but rather from the list object that the function returns. $ is actually an infix that takes two arguments, the values preceding and following it. It is a convenience function designed that uses non-standard evaluation of its second argument. It's called non-standard because the unquoted characters following $ are first quoted before being used to extract a named element from the first argument.

 t.test  # is the function
 t.test(x) # is a named list with one of the names being "p.value"
 

The value can be pulled in one of three ways:

 t.test(x)$p.value
 t.test(x)[['p.value']]  # numeric vector
 t.test(x)['p.value']  # a list with one item

 my.name.for.p.val <- 'p.value'
 t.test(x)[[ my.name.for.p.val ]]

When you surround a set of characters with flanking "%"-signs you can create your own vectorized infix function. If you wanted a pmax for which the defautl was na.rm=TRUE do this:

 '%mypmax%' <- function(x,y) pmax(x,y, na.rm=TRUE)

And then use it without quotes:

> c(1:10, NA) %mypmax% c(NA,10:1)
 [1]  1 10  9  8  7  6  7  8  9 10  1
1

First, the $ operator is for selecting an element of a list. See help('$').

The %% operator is the modulo operator. See help('%%').

-1

The '$' operator is used to select particular element from a list or any other data component which contains sub data components. For example: data is a list which contains a matrix named MATRIX and other things too. But to get the matrix we write, Print(data$MATRIX)

The %% operator is a modulus operator ; which provides the remainder. For example: print(7%%3) Will print 1 as an output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.