0

I'd like to use the [[ base R function to extract a column from a dataframe at the end of a sequence of piped operations. But I get an error message that doesn't make sense to me. Here's a simple example of the error message:

> mtcars |> `[[`("mpg")
Error: function '[[' not supported in RHS call of a pipe

This example is meant to accomplish the same result as the following:

mtcars[['mpg']]

Why doesn't the simple example mtcars |> `[[`("mpg") work? What can I do instead, using just base R?

bschneidr
  • 6,014
  • 1
  • 37
  • 52
  • You're right. My question is a duplicate which I will flag as such. – bschneidr Jun 07 '21 at 04:06
  • 1
    Of interest, dplyr `%>%` allows you to reference the [[ function in a manner that `|>` will not. (e.g. `mtcars %>% [[(.,'mpg')` --with of course the backticks around the [[. – SEAnalyst Jun 07 '21 at 04:52
  • Yep, that's why I posed the question. I was frustrated that the base R pipe wouldn't let me do something that the magrittr/dplyr pipe is very commonly used for. Appreciate you bringing this up. – bschneidr Jun 07 '21 at 06:41
  • `mtcars |> (\`[[\`)("mpg")` or `mtcars |> base::\`[[\`("mpg")` will work. – GKi Apr 25 '22 at 19:20

0 Answers0