r-如何通过数据帧迭代单个比例的z测试



在数据帧中迭代时,我可以用什么代码使下面代码中的x、n和p随每一行的变化而变化?

prop.test(x=5328960, n=12810000, p=0.416, alternative="two.sided")
# create dataframe with values of x, n, p
df <- data.frame(x=c(1000, 200, 3000),
n=c(5000, 600, 8000),
p=c(0.1, 0.2, 0.3),
random_col=c(1, 2, 3))
# create function to calculate prop.test for each row
prop_test = function(row, output) {
x = row[1]
n = row[2]
p = row[3]
return(prop.test(x=x, n=n, p=p, alternative="two.sided"))
}
# calculate prop.test for each row
apply(df, 1, prop_test)

最新更新