如何在数据框的每一行上执行一个函数,并且只将输出的一个元素作为该行中的新列插入 [英] 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

查看:34
本文介绍了如何在数据框的每一行上执行一个函数,并且只将输出的一个元素作为该行中的新列插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对两个值进行精确二项式检验很容易,但是如果想要对一大堆成功次数和试验次数进行检验会发生什么.我创建了一个测试敏感性数据框,研究中的潜在参与者数量,然后对于每一行,我计算成功的可能性.这是代码.

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 dataframe of test sensitivities, potential number of enrollees in a study and then for each row I calculate how may successes that would 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 now how do I use each row's combination of successes and number of trials to do the binomial test.

我只对二项式检验的 95% 置信区间的上限感兴趣.我希望将单个数字作为名为upper.limit"的列添加到数据框中

I am only interested in the upper limit of the 95% confidence interval of the binomial test. I want that single number to be added to the data frame as a column called "upper.limit"

我想到了一些与

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

唉,conf.int 给出了诸如

alas, conf.int gives something such as

[1] 0.1266556 0.2918427
attr(,"conf.level")
[1] 0.95

[1] 0.1266556 0.2918427
attr(,"conf.level")
[1] 0.95

我想要的只是 0.2918427

All I want is just 0.2918427

此外,我有一种感觉,必须在某处进行 do.call,甚至可能是 lapply,但我不知道这将如何贯穿整个数据框.或者我应该使用 plyr 吗?

Furthermore I have a feeling that there has to be do.call in there somewhere and maybe even an lapply but I do not know how that will go through the whole data frame. Or should I perhaps be using plyr?

显然我的头在旋转.请停止.

Clearly my head is spinning. Please make it stop.

推荐答案

如果这给了你(几乎)你想要的,那么试试这个:

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

这篇关于如何在数据框的每一行上执行一个函数,并且只将输出的一个元素作为该行中的新列插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆