简体   繁体   English

没有magrittr的dplyr博览会?

[英]exposition in dplyr without magrittr?

Does dplyr have a preferred syntax for magrittr's %$% operator, since this is not loaded by default in the tidyverse? dplyr是否具有magrittr的%$%运算符的首选语法,因为在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? 有没有一种首选的方法可以在单独的tidyverse中做到这一点? I feel like pull() should be able to do this, but I can't figure out how to call pull() twice inside the pipe. 我觉得pull()应该可以做到这一点,但我无法弄清楚如何在管道内调用两次pull()。

From vigenettes of magrittr : 来自magrittr的vigenettes

The “exposition” pipe operator, %$% exposes the names within the left-hand side object to the right-hand side expression. “exposition”管道运算符%$%将左侧对象中的名称暴露给右侧表达式。 Essentially, it is a short-hand for using the with functions. 从本质上讲,它是使用with函数的简写。

So you could do it with with : 所以你可以with

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

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)} 

In addition to @Marius solution, there is an option with summarise if we need to extract only the p-value 除了@Marius解决方案之外,如果我们只需要提取p-value ,还有一个summarise选项

 set.seed(24)
 data.frame(A=rbinom(100,1,p=0.5),B=rbinom(100,1,p=0.5)) %>% 
         summarise(p_val = chisq.test(A, B)$p.val)
 #     p_val
 #1 0.8394397

Suppose, we need to get other parameters, then use broom::tidy 假设,我们需要获取其他参数,然后使用broom::tidy

set.seed(24)
data.frame(A=rbinom(100,1,p=0.5),B=rbinom(100,1,p=0.5)) %>% 
    summarise(p_val = list(broom::tidy(chisq.test(A, B)))) %>%
    unnest
#  statistic   p.value parameter                                                       method
#1 0.0410509 0.8394397         1 Pearson's Chi-squared test with Yates' continuity correction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM