```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Genetic drift simulation in R - parts 3 and 4

## Step 1 - update to allow different population sizes

Modify the simulation function to accept multiples of 25 males and females:

```{r simulation.run.function.popsize}


```


Use an lapply() command to run the simulation 100 times:

```{r lapply.repeat.100.times.popsize}


```

Use an sapply() command to get the generation an allele went to fixation for each simulation:

```{r sapply.to.get.fixation.gen.popsize}



```

Use another sapply() command to get which allele it was that went to fixation:

```{r sapply.to.get.allele.fixed.popsize}




```

Combine the gen.fixed and allele.fixed into a data frame:

```{r fixation.data}



```

Get some statistics on the rate of fixation:

```{r fixation.stats}




```

Plot the generation fixed as a boxplot.

```{r gen.fixed.boxplot}




```


## Step 2 - add immigration

Edit the function to allow immigration.

```{r simulation.run.function.immigration}



```

Run the simulation 100 times, using 50 of each sex (multiplier of 2), and with immigration (immigration = TRUE)):

```{r run.100.times.with.immigration}



```

Use an sapply() command to find if allele was fixed at the final generation, and if so which allele it was:

```{r sapply.to.get.allele.fixed.imm}



```


Put allele.fixed into a data frame:

```{r fixation.data.imm}


```

Count up how often the alleles went to fixation:

```{r fixation.stats.imm}


```

Plot a line graph of the allele frequencies in the first run to see how immigration affected them: 

```{r allele.freq.immigration.graph}




```

Make a graph of heterozygosity:

```{r hz.immigration.graph}



```

Make a graph of allelic diversity:

```{r allelic.immigration.graph}



```