How do I perform a function on each row of a data frame and have just one element of the output inserted as a new column in that row

后端 未结 2 1537
余生分开走
余生分开走 2021-01-13 03:44

It is easy to do an Exact Binomial Test on two values but what happens if one wants to do the test on a whole bunch of number of successes and number of trials. I created a

2条回答
  •  忘掉有多难
    2021-01-13 04:37

    If this gives you (almost) what you want, then try this:

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

    And apply across the board or across the rows as it were:

    > 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
    

提交回复
热议问题