Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

%>% .$column_name equivalent for R base pipe |>

Tags:

I frequently use the dplyr piping to get a column from a tibble into a vector as below

iris %>% .$Sepal.Length
iris %>% .$Sepal.Length %>% cut(5)

How can I do the same using the latest R built-in pipe symbol |>

iris |> .$Sepal.Length
iris |> .$Sepal.Length |>  cut(5)
Error: function '$' not supported in RHS call of a pipe
like image 830
Afiq Johari Avatar asked Jun 02 '21 06:06

Afiq Johari


People also ask

What is the %>% operator in R?

%>% is called the forward pipe operator in R. It provides a mechanism for chaining commands with a new forward-pipe operator, %>%. This operator will forward a value, or the result of an expression, into the next function call/expression. It is defined by the package magrittr (CRAN) and is heavily used by dplyr (CRAN).

What does %>% mean in Tidyverse?

Use %>% to emphasise a sequence of actions, rather than the object that the actions are being performed on. Avoid using the pipe when: You need to manipulate more than one object at a time. Reserve pipes for a sequence of steps applied to one primary object.

Are pipes in base R?

The new pipe in R 4.1 It cuts down on external dependencies, so developers don't have to rely on an external package for such a key operation. Also, the built-in pipe may be faster. By contrast, grepl() in base R has the opposite syntax.

How do you type a pipe operator in R?

In RStudio the keyboard shortcut for the pipe operator %>% is Ctrl + Shift + M (Windows) or Cmd + Shift + M (Mac). In RStudio the keyboard shortcut for the assignment operator <- is Alt + - (Windows) or Option + - (Mac).


1 Answers

We can use getElement().

iris |> getElement('Sepal.Length') |> cut(5)
like image 175
jay.sf Avatar answered Sep 18 '22 21:09

jay.sf