我有一个大约 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/