R chi square test with degrees of freedom Does anybody know of a chi-square test function in R for which you can specify the degrees of freedom?
Example: I want to test if the Hardy-Weinberg distribution holds true for a sample of the MN blood group.
M <- 3156
MN <- 4997
N <- 1847
FM <- 2*M + MN
FN <- MN + 2*N

indiv.num <- M + MN + N
alleles.num <- (M + MN + N) * 2
alleles.num
p <- FM / alleles.num
q <- FN / alleles.num

M.exp <- p^2 * indiv.num
M.exp
MN.exp <- 2*p*q * indiv.num
MN.exp
N.exp <- q^2 * indiv.num
N.exp

chisq.test(c(M, MN, N), c(M.exp, MN.exp, N.exp))

chisq.test automatically calculates df=4:
    Pearson's Chi-squared test

data:  c(M, MN, N) and c(M.exp, MN.exp, N.exp)
X-squared = 6, df = 4, p-value = 0.1991

But this is not applicable for this case: df must be one, since 
from the frequency of one allele the frequency of the other allele is known.
 A: The question is about testing for Hardy-Weinberg equilibrium.  Genetically, that's the condition that the two alleles at a locus are independent. Statistically, its the condition that the number of alleles of each type for an individual has a Binom$(2,p)$ distribution.
In this case, the $M$ group has MM genotype and the $N$ group has NN genotype (would have been helpful to say this up front).
So the test is for $(M,MN,N)$ being the counts of the 0,1,2 outcomes from a Binom$(2,p)$ distribution (with estimated $p$) vs the alternative of a general trinomial distribution.
chisq.test() is not ideally suited to this: it's designed for either a contingency table or a fixed set of probabilities.
The data
> M <- 3156
> MN <- 4997
> N <- 1847


Estimate $p$
> FM <- 2*M + MN
> FN <- MN + 2*N
> 
> indiv.num <- M + MN + N
> alleles.num <- (M + MN + N) * 2
> alleles.num
[1] 20000
> p <- FM / alleles.num
> q <- FN / alleles.num

Work out the expected counts
> M.exp <- p^2 * indiv.num
> M.exp
[1] 3197.337
> MN.exp <- 2*p*q * indiv.num
> MN.exp
[1] 4914.326
> N.exp <- q^2 * indiv.num
> N.exp
[1] 1888.337

And now, finally, the test
> chisq.test(c(M, MN, N), p=c(M.exp, MN.exp, N.exp), rescale.p=TRUE)

    Chi-squared test for given probabilities

data:  c(M, MN, N)
X-squared = 2.8302, df = 2, p-value = 0.2429

The test statistic is correct, but the df isn't (because the function doesn't know there's an estimated parameter)
The $p$-value you want.
> pchisq(2.8302,df=1,lower.tail=FALSE)
[1] 0.09250684

It would be easier to just compute the statistic yourself
hwe<-function(AA,Aa,aa){

  Npeople<-AA+Aa+aa
  af<-(2*aa+Aa)/(2*Npeople)
  
  est.p <-dbinom(0:2, 2,af)

  chisq<-sum((c(AA,Aa,aa)-est.p*Npeople)^2/(Npeople*est.p))

c(chisq=chisq, p=pchisq(chisq,1,lower.tail=FALSE))
}

giving
> hwe(M,MN,N)
     chisq          p 
2.83015688 0.09250932 

(Going beyond diallelic markers or dizygotic species will take a little more effort)
A: I have the same problem. Using your code, if you calculate the chi-squared the hard way:
chisq_res <- ((M-M.exp)^2)/M.exp + ((MN-MN.exp)^2)/MN.exp + ((N-N.exp)^2)/N.exp

chisq_res = 2.83 and NOT 6. I think this is because the package expects contingency tables. But the genotype distribution is more complex.
Then, as suggested by machine, you can use:
p_val = pchisq(chisq_rs, df = 1, lower.tail = FALSE)

df = 3 (number of categories) - 1 (chi-squared) - 1 (because the genotype categories can be entirely defined by only 2 numbers, the third must add to the total).
A: If you want to know the p value for some chi-square value and a certain df you can find it out like that
# example
library(MASS)
tbl = table(survey$Smoke, survey$Exer) 
chisq.test(tbl)
# here you see that for df= 6 p is 0.4828

chi_val <- chisq.test(tbl)$statistic
pchisq(chi_val,df= 6,lower.tail=FALSE)
# gives you the same p value

In your case with a chi-square value of 6 and df= 1 you can use pchisq(x= 6,df= 1,lower.tail=FALSE), which is 0.01430588. Here, I set Chi-square to $6$ with x= 6 and the degrees of freedom to $1$ by using df= 1 to match your example.
Please note that I am not sure what your code above is doing. Your variables are saying ".exp", but we do not need to provide expected values for the chisq.test function unless you await certain probabilities (See answer from Sal Mangiafico). This means if your question is whether chi-square for the frequencies c(M, MN, N) is significant, you can use chisq.test(c(M, MN, N)). But here df is 2 since the vector has length 3.
A: It looks like you are trying to perform a chi-square goodness-of-fit test.  That's about all I can suss out, so I can't say the following is correct for your purposes.  In this case, the last line of your code could be replaced with the following.  My advice is to perform the desired test by hand, or to compare to a published example similar to yours, and then compare the R output to be sure the code is doing what you want.
Theoretical = c(M.exp  / sum(M.exp, MN.exp, N.exp), 
                MN.exp / sum(M.exp, MN.exp, N.exp),
                N.exp  / sum(M.exp, MN.exp, N.exp))

Theoretical

chisq.test(c(M, MN, N), p= Theoretical) 

