Very simple chi-square question I have a super simple  chi-square in R question, but I'm struggling to wrap my head around it. The question was to take a random 20 people from the dataset, count males and females and then use chi-square test to accept/reject the null hypothesis that half of the population in the entire large dataset is men.
So this is my data table:

How can I run chisq.test on just 2 numbers? Or should there be an 'expected' column with values of 10 for each row and then run the function? I haven't been able to find a similar chi-square problem on the internet.
 A: Your problem can be seen as having an iid sample $X_1,\ldots,X_n$ with $X_i\sim \text{Bernoulli}(\theta)$, with say $X_i=1$ if the sample is female and $X_i=0$ otherwise. In this notation, $\theta$ is the probability of observing a female.
The aim is to test $H_0:\theta=1/2$ vs $H_1:\theta\neq 1$.
There are many ways to test $H_0$, e.g. through a hypothesis testing procedure or a confidence interval. Both can be obtained by noting that if $\hat\theta = \bar X$ is the sample proportion of females, then, under $H_0$
$$
n\bar X \sim \text{Bin}(n,\theta_0),
$$
which can be used to perform a test statistic or it may be inverted to obtain a confidence interval. There are many ways to perform such inversion and an exact R implementation is
> x <- 8
> n <- 20
> binom.test(x, n, p=0.50)

    Exact binomial test

data:  x and n
number of successes = 8, number of trials = 20, p-value = 0.5034
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
 0.1911901 0.6394574
sample estimates:
probability of success 
                   0.4 

You might want to have a look also at the exactci package for other related approaches.
Another path may be to invoke the Central Limit Theorem, i.e.
$$
T_n = \frac{\sqrt{n}(\bar X - \theta)}{\sqrt{\theta(1-\theta)}}\overset{d}{\to} N(0,1).\quad\quad(*)
$$
Under $H_0$, $(*)$ is a pivotal quantity and can be used to build the approximate $\alpha$-level test statistic:

Reject $H_0$ if the observed value of $T_n$ is in absolute value
greater than $z_{1-\alpha/2}$.

Now for large $n$, $T_n^2$ will be approximately $\chi_1^2$, thus another equivalent $\alpha$-level test statistics would be

Reject $H_0$ if the observed value of $T_n^2$ is
greater than $\chi_{1-\alpha}^2$.

In R this can be performed (using a continuity correction for better accuracy) by
> x <- 8
> n <- 20
> prop.test(x, n, p=0.5)

    1-sample proportions test with continuity correction

data:  x out of n, null probability 0.5
X-squared = 0.45, df = 1, p-value = 0.5023
alternative hypothesis: true p is not equal to 0.5
95 percent confidence interval:
 0.1997709 0.6358833
sample estimates:
  p 
0.4 

A: As with any table, the chi-squared statistic is the sum of $(O-E)^2/E$ where $O$ is the observed count in each cell and $E$ is the expected count.
In your case, there are two cells with observations $12$ and $8.$  The null hypothesis asserts that the expected counts are each $1/2$ times the sample size, $E = 1/2\times 20 = 10.$  Thus
$$\chi^2 = \frac{(12 - 10)^2}{10} + \frac{(8 - 10)^2}{10} = \frac{8}{10}.$$
The p-value (for the two-sided alternative to the null; namely, that the population proportion of males differs from $1/2$) is approximated by the right tail area of a chi-squared distribution.  The one to use has $2-1 = 1$ degrees of freedom, because (a) you have two cells and (b) the null hypothesis specifies one parameter, leaving one left over. The p-value, computed with the R function pchisq(8/10, 1, lower.tail = FALSE), is 37%.

Some have contended this is the "wrong" test.  Far from it, this is an excellent test.  Part of the proof is to consider the distribution of the possible p-values under the null hypothesis.  (The other part is to examine its power, but that would take us far afield.)  Ideally, the p-values will be uniformly distributed between $0$ and $1.$ Because there are only $11$ distinct outcomes ($0$ and $20$ lead to the same decision, $1$ and $19$ to the same, and so on) the ideal is impossible to attain for any (non-randomized) decision procedure whatsoever.  But how close can we come?  Look at the actual distribution function:

The reference uniform distribution appears as the dashed red line.  The plot at left shows that the null distribution of p-values comes as close as reasonably possible to the uniform reference distribution, at least insofar as we can see.  (In the very rare cases where one of the cells is zero, I have set the p-value to zero.)
The plot at the right is the same, shown on log-log axes to reveal details for small p-values.  From left to right are the outcomes $0,20; 1,19; 2,18;$ and so on.  Except at the extreme left, where one of the cell counts is only $0$ or $1,$ the p-values are approximately uniformly distributed.  Moreover, this failure at the left is minor: in almost any case you would correctly reject the null even though the p-value is a little larger than it ought to be.
A standard rule of thumb, by the way, is that you can trust the chi-squared test when all expected counts are $5$ or larger.  In this case, both expected counts are $10:$ the rule gives good advice.
A: You are using the wrong test.  While you could run chi squared test (with expected values of 10 and 10) and 1 degree of freedom, there is a much better way.  You haven't found examples like this because you should use a binomial test instead.
Given the null hypothesis (and quite reasonable assumptions on the large size of the population and the sampling method) the number of females would have a binomial distribution  B(20,0.5).
So you use the binomial distribution to find a (say) 95% acceptance region.  The exact region depends on if you want a two-tail or one-tail test. But 6≤n≤14 works for me.
And then reject your null hypothesis if the observed value of female lies outside that region.
