# In R, fisher.test returns different results if I use vectors vs contingency table

Using R, I noticed that I get different results if I use fisher.test() with raw data (as factors) versus a contingency table. The documentation states that fisher.test(x,y) will compute a contingency table from x, y by treating them as factors.

My example is a baseline statistics exercise: comparing the gender split in a placebo group to the experimental group.

Here's my example for running fisher.test on factors:

> expData <- data.frame(Placebo=factor(c("Y","Y","Y","Y","N","N","N","N")),
Gender=factor(c("F","F","F","M","M","M","M","F")))
> expData
Placebo Gender
1       Y      F
2       Y      F
3       Y      F
4       Y      M
5       N      M
6       N      M
7       N      M
8       N      F
> fisher.test(expData[expData$Placebo=="Y","Gender"], expData[expData$Placebo=="N","Gender"])

Fisher's Exact Test for Count Data

data:  expData[expData$Placebo == "Y", "Gender"] and expData[expData$Placebo == "N", "Gender"]
p-value = 0.25
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
0.00000 13.00002
sample estimates:
odds ratio
0


Here's my example for building a contingency table representing the above data and running fisher.test on that:

> contingency <- matrix(c(3,1,1,3),nrow=2,
dimnames = list(tx=c("Placebo","Exper"), gender=c("F","M")))
> contingency
gender
tx        F M
Placebo 3 1
Exper   1 3

> fisher.test(contingency)

Fisher's Exact Test for Count Data

data:  contingency
p-value = 0.4857
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
0.2117329 621.9337505
sample estimates:
odds ratio
6.408309


Am I using fisher.test in the wrong way? Or maybe building my contingency table wrong?

> fisher.test(expData[,1], expData[,2])

Fisher's Exact Test for Count Data

data:  expData[, 1] and expData[, 2]
p-value = 0.4857
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
0.001607888 4.722931239
sample estimates:
odds ratio
0.156047


as per the doc: x is the outcome and y is the factor (or vice-versa).