Home > Software design >  Apply BinomCI over rows in a dataframe
Apply BinomCI over rows in a dataframe

Time:09-17

I would like to apply a function which uses two columns and one row at a time as the example below shows

nine<-data.frame(N<-c(118, 147,  63,  77, 141,  93, 639,  54,121, 143, 321, 639, 117, 522, 639,287,232, 120, 639),B<-c(96, 103,  55,  62, 111,  76, 503,  37,94, 118, 254, 503,  93, 410, 503, 214, 192,  97, 503))




require(DescTools) # 




xx2<- BinomCI(nine$B[2], nine$N[2], 
    conf.level = 0.95,
    method = "clopper-pearson")
print(round(xx2,3)*100)

I need to repeat this for all the rows and put into one table without doing it 19 times

CodePudding user response:

In {dplyr} we can just use summarise and wrap BinomCI in as_tibble. We don't need a loop, do.call or Map, which makes this really readable:

library(dplyr)
library(DescTools) 

nine %>% 
  summarise(as_tibble(BinomCI(B, N, conf.level = 0.95, method = "clopper-pearson")))
 
#>          est    lwr.ci    upr.ci
#> 1  0.8135593 0.7314461 0.8793143
#> 2  0.7006803 0.6197144 0.7733586
#> 3  0.8730159 0.7650344 0.9435487
#> 4  0.8051948 0.6991329 0.8866683
#> 5  0.7872340 0.7104059 0.8516066
#> 6  0.8172043 0.7235378 0.8897553
#> 7  0.7871674 0.7533573 0.8182984
#> 8  0.6851852 0.5444760 0.8047606
#> 9  0.7768595 0.6921962 0.8475055
#> 10 0.8251748 0.7528350 0.8835509
#> 11 0.7912773 0.7426642 0.8344236
#> 12 0.7871674 0.7533573 0.8182984
#> 13 0.7948718 0.7103353 0.8639278
#> 14 0.7854406 0.7476979 0.8199146
#> 15 0.7871674 0.7533573 0.8182984
#> 16 0.7456446 0.6911363 0.7950020
#> 17 0.8275862 0.7727093 0.8738764
#> 18 0.8083333 0.7264176 0.8744483
#> 19 0.7871674 0.7533573 0.8182984

Created on 2021-09-14 by the reprex package (v2.0.1)

CodePudding user response:

You may use any of the apply loop from base R. For eg with Map.

library(DescTools)

do.call(rbind.data.frame, Map(function(x, y) 
          BinomCI(x, y, conf.level = 0.95, method = "clopper-pearson"), 
          nine$B, nine$N))

#         est    lwr.ci    upr.ci
#1  0.8135593 0.7314461 0.8793143
#2  0.7006803 0.6197144 0.7733586
#3  0.8730159 0.7650344 0.9435487
#4  0.8051948 0.6991329 0.8866683
#5  0.7872340 0.7104059 0.8516066
#6  0.8172043 0.7235378 0.8897553
#7  0.7871674 0.7533573 0.8182984
#8  0.6851852 0.5444760 0.8047606
#9  0.7768595 0.6921962 0.8475055
#10 0.8251748 0.7528350 0.8835509
#11 0.7912773 0.7426642 0.8344236
#12 0.7871674 0.7533573 0.8182984
#13 0.7948718 0.7103353 0.8639278
#14 0.7854406 0.7476979 0.8199146
#15 0.7871674 0.7533573 0.8182984
#16 0.7456446 0.6911363 0.7950020
#17 0.8275862 0.7727093 0.8738764
#18 0.8083333 0.7264176 0.8744483
#19 0.7871674 0.7533573 0.8182984

CodePudding user response:

Anyone noticed that BinomCI is vectorized? You don't even need a screwdriver here :)

BinomCI(nine$B, nine$N, conf.level = 0.95, method = "clopper-pearson")
#                 est    lwr.ci    upr.ci
# x.1:n.1   0.8135593 0.7314461 0.8793143
# x.2:n.2   0.7006803 0.6197144 0.7733586
# x.3:n.3   0.8730159 0.7650344 0.9435487
# x.4:n.4   0.8051948 0.6991329 0.8866683
# x.5:n.5   0.7872340 0.7104059 0.8516066
# x.6:n.6   0.8172043 0.7235378 0.8897553
# x.7:n.7   0.7871674 0.7533573 0.8182984
# x.8:n.8   0.6851852 0.5444760 0.8047606
# x.9:n.9   0.7768595 0.6921962 0.8475055
# x.10:n.10 0.8251748 0.7528350 0.8835509
# x.11:n.11 0.7912773 0.7426642 0.8344236
# x.12:n.12 0.7871674 0.7533573 0.8182984
# x.13:n.13 0.7948718 0.7103353 0.8639278
# x.14:n.14 0.7854406 0.7476979 0.8199146
# x.15:n.15 0.7871674 0.7533573 0.8182984
# x.16:n.16 0.7456446 0.6911363 0.7950020
# x.17:n.17 0.8275862 0.7727093 0.8738764
# x.18:n.18 0.8083333 0.7264176 0.8744483
# x.19:n.19 0.7871674 0.7533573 0.8182984
  • Related