2017-06-28 44 views
6

現在purrr是by_row()將是貶值的,什麼是新的首選tidyverse執行情況(是什麼?):rowwise()do()與purrr等效,現在該by_row()已折舊?

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

看來,如果你還不巢每一行成一列,並然後使用map(),但我不知道如何做嵌套操作...再加上它似乎有點模糊。有沒有更好的辦法?

回答

5

這裏是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 
+0

是否在PMAP函數調用,您可以傳遞參數的一種方式?例如,如果你希望binom.test中的「p」參數是「c-0.5」,我想要做一些像pmap(binom.test(p = .z-0.5))但顯然不會沒有工作。有沒有相同的東西? –

+0

@NicholasRoot我想你需要'pmap(〜binom.test(。,p = z -0.5))' – akrun

+1

請注意,如果你使用'somedata'中的列名稱來匹配參數函數(在這種情況下爲'binom.test')。這將更加明確,因此可能更安全。 – cboettig