How to execute a function in each row of a data frame and have only one output element inserted as a new column in this row

It is easy to perform an accurate binomial test in two ways, but what happens if you want to test for a whole bunch of successes and tests. I created a dataframe of test sensitivity, the potential number of students enrolled in the study, and then for each row I calculate how success can be. Here is the code.

sens <-seq(from=.1, to=.5, by=0.05)
enroll <-seq(from=20, to=200, by=20)
df <-expand.grid(sens=sens,enroll=enroll)
df <-transform(df,succes=sens*enroll)

But how can I use each combination of strings from successes and the number of tests to perform a binomial test.

I'm only interested in the upper limit of the 95% confidence interval of the binomial test. I want this single number to be added to the data frame as a column named "upper.limit"

I was thinking about something in the lines

binom.test(succes,enroll)$conf.int    

alas, conf.int -

[1] 0.1266556 0.2918427
( "conf.level" )
[1] 0,95

, , 0.2918427

, , do.call - , , , , . plyr?

, . , .

+5
2

() , , :

binom.test(succes,enroll)$conf.int[2]

:

> df$UCL <- apply(df, 1, function(x)  binom.test(x[3],x[2])$conf.int[2] )
> head(df)
  sens enroll succes       UCL
1 0.10     20      2 0.3169827
2 0.15     20      3 0.3789268
3 0.20     20      4 0.4366140
4 0.25     20      5 0.4910459
5 0.30     20      6 0.5427892
6 0.35     20      7 0.5921885
+9

:

R> newres <- do.call(rbind, apply(df, 1, function(x) { 
+                     bt <- binom.test(x[3], x[2])$conf.int; 
+                     newdf <- data.frame(t(x), UCL=bt[2]) }))
R>
R> head(newres)
  sens enroll succes     UCL
1 0.10     20      2 0.31698
2 0.15     20      3 0.37893
3 0.20     20      4 0.43661
4 0.25     20      5 0.49105
5 0.30     20      6 0.54279
6 0.35     20      7 0.59219
R> 

apply, , , , () data.frame. 90 data.frame do.call(rbind, ...) , apply.

, , , , . , data.frame apply.

+1

All Articles