2017-06-28 18 views
6

अब जब कि by_row() purrr में होने जा रहा है मूल्यह्रास, की नई वरीयता tidyverse कार्यान्वयन है क्या (है?):rowwise के समतुल्य()() 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()) 

यह रूप में यदि आप एक ही स्तंभ में घोंसला प्रत्येक पंक्ति हो सकता है लगता है, और फिर मानचित्र() का उपयोग करें, लेकिन मुझे यकीन नहीं है कि उस घोंसले के संचालन को कैसे करना है ... और ऐसा लगता है कि यह थोड़ा अस्पष्ट है। क्या कोई बेहतर तरीका है?

उत्तर

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 में समारोह कॉल आप तर्क पारित करने के लिए अनुमति नहीं है के साथ के साथ एक तरीका है? उदाहरण के लिए, यदि इसके बजाय आप को "सी 0.5", मैं pmap की तरह कुछ करना चाहता हूँ चाहते हैं (binom.test (p = .z-0.5)), लेकिन है कि स्पष्ट रूप से नहीं करता है binom.test में "पी" तर्क चाहता था काम नहीं करता क्या समकक्ष है? –

+0

@NicholasRoot मुझे तुम्हारी जरूरत है 'pmap लगता है (~ binom.test (।, पी = z -0.5))' – akrun

+1

ध्यान दें कि आप 'unname' से बच सकते हैं अगर आप' somedata' में स्तंभ नाम है कि के तर्कों से मेल का उपयोग फ़ंक्शन (इस मामले में 'binom.test')। यह अधिक स्पष्ट और संभवतः सुरक्षित होगा। – cboettig

संबंधित मुद्दे