r - 在大型 data.frame 上按行进行 prop.test

标签 r

我有一个大约 1000 行和 2 列的 data.frame。我想执行 prop.test(k, n) 其中 k == 我的 data.frame 的 Column1,n == 我的 data.frame 的 Column2。

例如:

 Column1(k)   Column2(n)      
     60         500    
     50         500     
     70         500    
     40         500     

我想对每一行执行 prop.test (k, n) 。例如:

prop.test(60, 500)
prop.test(50, 500)
prop.test(70, 500)

等等。因为我有大约 1000 行,所以我显然无法逐行手动执行 prop.test 。 如何编写一个每次将每一行作为输入并执行 prop.test 的函数?

非常感谢

E.

最佳答案

您可以使用 Map,它是 mapply 的包装器:

dfr <- data.frame(k=c(60,50,70,40),n=rep(500,4))
Map(prop.test,x=dfr$k,n=dfr$n)
[[1]]

        1-sample proportions test with continuity correction

data:  dots[[1L]][[1L]] out of dots[[2L]][[1L]], null probability 0.5 
X-squared = 287.282, df = 1, p-value < 2.2e-16
alternative hypothesis: true p is not equal to 0.5 
95 percent confidence interval:
 0.09348364 0.15251247 
sample estimates:
   p 
0.12 


[[2]]

        1-sample proportions test with continuity correction

data:  dots[[1L]][[2L]] out of dots[[2L]][[2L]], null probability 0.5 
X-squared = 318.402, df = 1, p-value < 2.2e-16
alternative hypothesis: true p is not equal to 0.5 
95 percent confidence interval:
 0.07580034 0.13052865 
sample estimates:
  p 
0.1 

...

请注意,prop.test 在数据解析方面存在问题,因此很难识别哪个是哪个。

关于r - 在大型 data.frame 上按行进行 prop.test,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16944956/

相关文章:

r - 如何使用 geom_text 在堆积条形图中显示值?

r - R bookdown-自定义标题页

r - 带有 HTML 的 Shiny 、ggvis 和 add_tooltip

r - 使用列的分类值删除R中的索引

r - 将函数应用于列的可变子集

r - 如何根据字符(小于号, "<")的出现重新计算数据框中的值?

r - 相当快地保存非常大的 R 数据帧

r - 在 R 图中快速插入缺失值

r - dplyr 在变量子集上使用 pivot_longer 和 pivot_wider

r - 使用 model.matrix 进行单热编码