2017-06-28 11 views
6

Şimdi bu by_row() yeni tercih tidyverse uygulamasıdır ne amortismana tabi olacak: Size tek sütuna yuva her satır olabilir sanki görünüyorpwt ile rowwise() do() eşdeğeri, şimdi by_row() değer kaybına uğruyor mu? (Mi?) Purrr içinde

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

ve sonra map() kullanın, ama bu yuvalama işlemini nasıl yapacağından emin değilim ... ve bu biraz karanlık gibi görünüyor. Daha iyi bir yolu var mı? İşte

cevap

5

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 

Veya sadece tidyverse fonksiyonları

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 işlevi çağrısı argümanları geçmek izin mu ile map ile bir yoludur? Örneğin, bunun yerine "p" argümanının binom.test dosyasında "c-0.5" olmasını istediyseniz, pmap (binom.test (p = .z-0.5)) gibi bir şey yapmak isterdim, fakat açıkçası Çalışıyorum Eşdeğer var mı? –

+0

@NicholasRoot Sanırım pmap (~ binom.test (., P = z -0.5)) ' – akrun

+1

' unda'ndan kaçınmanız gerektiğini unutmayın. işlevi (bu durumda binom.test). Bu daha açık ve muhtemelen daha güvenli olurdu. – cboettig

İlgili konular