繁体   English   中英

与purrr等效的rowwise()do(),现在不推荐使用by_row()?

[英]Equivalent of rowwise() do() with purrr, now that by_row() is deprecated?

既然purrr中的by_row()将被弃用,那么新的首选tidyverse实现是什么:

somedata = expand.grid(a=1:3,b=3,c=runif(3))
somedata %>%
  rowwise() %>% do(binom.test(x=.$a,n=.$b,p=.$c) %>% tidy())

看起来好像你可能将每一行嵌套到一个列中,然后使用map(),但我不确定如何进行嵌套操作......再加上它看起来有点模糊。 有没有更好的办法?

这是map一种方式

library(tidyverse)
library(broom)
do.call(Map, c(f = binom.test, unname(somedata))) %>%
      map_df(tidy)
#  estimate statistic    p.value parameter    conf.low conf.high              method alternative
#1 0.3333333         1 1.00000000         3 0.008403759 0.9057007 Exact binomial test   two.sided
#2 0.6666667         2 0.25392200         3 0.094299324 0.9915962 Exact binomial test   two.sided
#3 1.0000000         3 0.03571472         3 0.292401774 1.0000000 Exact binomial test   two.sided
#4 0.3333333         1 0.14190440         3 0.008403759 0.9057007 Exact binomial test   two.sided
#5 0.6666667         2 0.55583967         3 0.094299324 0.9915962 Exact binomial test   two.sided
#6 1.0000000         3 1.00000000         3 0.292401774 1.0000000 Exact binomial test   two.sided
#7 0.3333333         1 0.58810045         3 0.008403759 0.9057007 Exact binomial test   two.sided
#8 0.6666667         2 1.00000000         3 0.094299324 0.9915962 Exact binomial test   two.sided
#9 1.0000000         3 0.25948735         3 0.292401774 1.0000000 Exact binomial test   two.sided

或者只有tidyverse功能

somedata %>%
     unname %>%
     pmap(binom.test) %>% 
     map_df(tidy)
#estimate statistic    p.value parameter    conf.low conf.high              method alternative
#1 0.3333333         1 1.00000000         3 0.008403759 0.9057007 Exact binomial test   two.sided
#2 0.6666667         2 0.25392200         3 0.094299324 0.9915962 Exact binomial test   two.sided
#3 1.0000000         3 0.03571472         3 0.292401774 1.0000000 Exact binomial test   two.sided
#4 0.3333333         1 0.14190440         3 0.008403759 0.9057007 Exact binomial test   two.sided
#5 0.6666667         2 0.55583967         3 0.094299324 0.9915962 Exact binomial test   two.sided
#6 1.0000000         3 1.00000000         3 0.292401774 1.0000000 Exact binomial test   two.sided
#7 0.3333333         1 0.58810045         3 0.008403759 0.9057007 Exact binomial test   two.sided
#8 0.6666667         2 1.00000000         3 0.094299324 0.9915962 Exact binomial test   two.sided
#9 1.0000000         3 0.25948735         3 0.292401774 1.0000000 Exact binomial test   two.sided

暂无
暂无

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

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