Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exposition in dplyr without magrittr?

Tags:

r

dplyr

Does dplyr have a preferred syntax for magrittr's %$% operator, since this is not loaded by default in the tidyverse? For example, I often use this:

data.frame(A=rbinom(100,1,p=0.5),B=rbinom(100,1,p=0.5)) %$% chisq.test(A,B)

Is there a preferred way to do this within the tidyverse alone? I feel like pull() should be able to do this, but I can't figure out how to call pull() twice inside the pipe.

like image 841
Nicholas Root Avatar asked Jan 28 '23 20:01

Nicholas Root


2 Answers

From vigenettes of magrittr:

The “exposition” pipe operator, %$% exposes the names within the left-hand side object to the right-hand side expression. Essentially, it is a short-hand for using the with functions.

So you could do it with with:

data.frame(A=rbinom(100,1,p=0.5),B=rbinom(100,1,p=0.5)) %>% with(chisq.test(A,B))
like image 118
mt1022 Avatar answered Feb 04 '23 15:02

mt1022


You can use a dot . to refer to the original dataframe, and use curly braces {} to prevent %>% filling in the first argument:

data.frame(A=rbinom(100,1,p=0.5),B=rbinom(100,1,p=0.5)) %>% 
    {chisq.test(.$A, .$B)} 
like image 26
Marius Avatar answered Feb 04 '23 13:02

Marius