You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过流程从fisher.test()结果中提取指定p值?

Extracting the First p-value from Fisher's Exact Test in R

Let's break down how to create a contingency table, run Fisher's exact test, and pull out that specific p-value (0.7761301) you need. Here's a step-by-step guide:

1. Create Your Contingency Table

First, generate a cross-tab from your dataset. Let's assume you have two categorical variables (e.g., group and outcome) in a data frame called df:

# Using base R's table() function
cross_tab <- table(df$group, df$outcome)

# Alternatively, use xtabs() for formula-based syntax
cross_tab <- xtabs(~ group + outcome, data = df)

2. Run Fisher's Exact Test

Next, pass your contingency table to fisher.test() and store the result:

fisher_output <- fisher.test(cross_tab)

The fisher_output object is a list containing all the test results—including the p-value we need.

3. Extract the p-value

To get the p-value, simply access the p.value element of the result list. If you need it formatted to match the 7-decimal value you mentioned (0.7761301), use round():

# Extract raw p-value
raw_p <- fisher_output$p.value

# Round to 7 decimal places to match your target value
formatted_p <- round(raw_p, 7)

# Print the result
print(formatted_p) # Output: 0.7761301

If You're Running Multiple Tests

If you're applying Fisher's test to multiple contingency tables (e.g., stored in a list), you'd extract the first result's p-value like this:

# Example: List of contingency tables
cross_tabs_list <- list(table1, table2, table3)

# Run test on all tables
fisher_results <- lapply(cross_tabs_list, fisher.test)

# Get p-value from the first test result
first_p_value <- fisher_results[[1]]$p.value

That's all! The key here is remembering that fisher.test() returns a list, so you can directly access its elements with $ or [[ notation.

内容的提问来源于stack exchange,提问作者Andrej

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:01:20