2
library(tidyverse)
mtcars %>% .[1:2, ]

When I %>% pipe data sets forward I usually use the . period to accomplish the task. An example is provided above. I've noticed many other people do the same utilizing the period ..

Recently, I was provided an answer on stackoverflow, with a functionally equivalent `[` (and a little other swappage) as shown below.

mtcars %>% `[`(1:2, )

What is this `[`? And if you can in the comments please recommend a way to search for such things on Google, Bing, etc. When you search Google for `[` not much comes up.

Display name
  • 4,153
  • 5
  • 27
  • 75
  • 6
    `\`[\`` is the actual function. `[]` is how you use it. Why won't you just type `?\`[\`` in R console? Or just `\`[\`` to see it's source code. I would guess it's a close dupe of [this](https://stackoverflow.com/questions/10449366/levels-what-sorcery-is-this) – David Arenburg Jun 10 '19 at 13:22
  • 2
    The use of backticks is required for some functions that normally have a special character in them, often with internal functions such as indexing (`[`, `$`, `[[`) and functions with spaces in names (not common). On the R console, `'['` and `"["` both return strings, so the backtick is a way of saying *"not a string, something **special**"*. – r2evans Jun 10 '19 at 13:25
  • 1
    These parentheses are accessor methods for specific objects and can be used like a normal function, by using the backquotes. In that case, the first argument of the function is the object itself and should be omitted in a dplyr chain. You may find the following useful: http://adv-r.had.co.nz/Functions.html#all-calls – amatsuo_net Jun 10 '19 at 13:27
  • 1
    JasonHunter, DSGym's link is a good explanation of most of it. To tie to my first comment, the `<-` component of that function is what makes it "special". Whereas you can use `levels` (using that example) by itself, the `<-` requires that you tell R to not parse the `<-` separately, so `\`levels<-\`` (with backticks) is the way to access that special factor-utility function. – r2evans Jun 10 '19 at 13:45

2 Answers2

2

A function in R is invoked using fun(arg1, arg2, ...) but certain functions can be invoked using different syntax. When we write BOD[1, 2] what we are really doing is invoking the [ function on the arguments BOD, 1 and 2 and this can be alternately written as a normal function invocation. Because [ uses a special character that is not normally allowed in object names we must surround it with backticks to tell R to regard it as a name. It can also be specified as a constant string. Thus these are all the same:

BOD[1, 2]
`[`(BOD, 1, 2)  # same
"["(BOD, 1, 2)  # same
'['(BOD, 1, 2)  # same

examples

Here are other examples of this:

1 + 2
`+`(1, 2)  # same

3 %in% 2:4
`%in%`(3, 2:4)  # same

if (2 > 3) 4 else 5
`if`(2 > 3, 4, 5)  # same

getAnywhere

We can find the code of a function using getAnywhere like this:

getAnywhere(`[`)

but in this case it is just a primitive so we get:

A single object matching ‘[’ was found
It was found in the following places
  package:base
  namespace:base
with value

.Primitive("[")

Actually, in this case what [ does when the first argument is a data frame is to invoke [.data.frame and that one has R source so we do this to see its source:

getAnywhere(`[.data.frame`)

In some cases getAnywhere finds several occurrences of a name. In that case it will tell you where it found each and to get the ith use getAnywhere(...)[i] where ... is the name you are looking for.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
0

The reference to the code line number is valid for R 3.6.0.
The answer is in the sources. In line 605 of file src/library/base/R/dataframe.R you can see the definition of the extraction method [ for objects of class "data.frame".

`[.data.frame` <-
    function(x, i, j, drop = if(missing(i)) TRUE else length(cols) == 1)
{
    # function body
}

So the generic is '[' and what the magrittr pipe is calling is the above method.

library(magrittr)

subdf1 <- mtcars %>% `[`(1:2, )
subdf2 <- `[`(mtcars, 1:2, )
subdf3 <- `[.data.frame`(mtcars, 1:2, )

identical(subdf1, subdf2)
#[1] TRUE
identical(subdf2, subdf3)
#[1] TRUE

In the first example, the pipe is used.
In the second example the generic extraction function is called and the S3 dispatch mechanism calls the appropriate method.
In the third example the data.frame method is called directly.

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66