在qwraps::summary_表中添加p值列
我想使用在qwraps::summary_表中添加p值列,r,r-markdown,qwraps2,R,R Markdown,Qwraps2,我想使用qwraps::summary_table为我的同事制作一个小的R-Markdown汇总表。data.frame包含不同曝光的信息。所有变量都编码为二进制 library(qwraps2) library(dplyr) pop <- rbinom(n = 1000, size = 1, prob = runif(n = 10, min = 0, max = 1)) exp <- rbinom(n = 1000, size = 1, prob = .5) ID <-
qwraps::summary_table
为我的同事制作一个小的R-Markdown汇总表。data.frame包含不同曝光的信息。所有变量都编码为二进制
library(qwraps2)
library(dplyr)
pop <- rbinom(n = 1000, size = 1, prob = runif(n = 10, min = 0, max = 1))
exp <- rbinom(n = 1000, size = 1, prob = .5)
ID <- c(1:500)
therapy <- factor(sample(x = pop, size = 500, replace = TRUE), labels = c("Control", "Intervention"))
exp_1 <- sample(x = exp, size = 500, replace = TRUE)
exp_2 <- sample(x = exp, size = 500, replace = TRUE)
exp_3 <- sample(x = exp, size = 500, replace = TRUE)
exp_4 <- sample(x = exp, size = 500, replace = TRUE)
df <- data.frame(ID, exp_1, exp_2, exp_3, exp_4, therapy)
head(df)
库(qwraps2)
图书馆(dplyr)
pop[已解决]
同时,在对这个主题进行了大量研究之后,我找到了一种方便易行的方法来添加p.values列。也许这不是最聪明的解决方案,但至少对我来说是有效的
首先,我用一个函数计算p.values,该函数从fisher.test的返回输出中提取p.values,并将其存储在一个对象中,在我的例子中是一个简单的数字向量:
# write function to extract fishers.test
fisher.pvalue <- function(x) {
value <- fisher.test(x)$p.value
return(value)
}
# fisher test/generate pvalues
p.vals <- round(sapply(list(
table(df$exp_1, df$therapy),
table(df$exp_2, df$therapy),
table(df$exp_3, df$therapy),
table(df$exp_4, df$therapy)), fisher.pvalue), digits = 2)
在我的例子中,我只是在totall_table
(对于p-Values
=9:12)中查找相应的单元格索引,并使用基本语法填充它们。在qwraps2
()的小插曲中,作者使用正则表达式来识别正确的单元格(参见第3.2节)
如果有其他方法可以将单个列添加到qwraps2::summary_表中
,我将非常感谢您的帮助
最佳,
弗洛里安
# write function to extract fishers.test
fisher.pvalue <- function(x) {
value <- fisher.test(x)$p.value
return(value)
}
# fisher test/generate pvalues
p.vals <- round(sapply(list(
table(df$exp_1, df$therapy),
table(df$exp_2, df$therapy),
table(df$exp_3, df$therapy),
table(df$exp_4, df$therapy)), fisher.pvalue), digits = 2)
overall_table <- cbind(my_table, "P-Value" = "") # create empty column
overall_table[9:12] <- p.vals # add vals to empty column
# overall_table <- cbind(my_table, "P-Value" = p.vals) works the same way in one line of code
overall_table