I am trying to create a sandpile model in r. It is one dimensional.

The rules are as follows:

- There are 10 spaces.
- Pick a random space. Drop a grain of sand.
- a. If the space has 0 grains of sand, add 1 grain of sand, for a total of 1. b. If it has 1 grain of sand, it now has two and the pile collapses. Distribute one of these grains to each neighbour. If the neighbour has 0 grains of sand, add one. If it has two grains of sand, distribute one to each neighbour. Continue until no more spaces have more than 1 grain. The grains fall off the edge at 0 and 11.

Now I wrote some code. Generating the initial state itself was easy of course:

```
sandpile <- c(0,0,0,0,0,0,0,0,0,0)
```

After figuring out square brackets, I managed to randomly drop sand and update the neighbouring coordinates:

```
drop <- sample (1:10, 1)
sandpile[drop] <- sandpile[drop]+1
if(sandpile[drop] > 1) { (sandpile[drop-1] <- sandpile[drop-1]+1)
if(sandpile[drop] > 1 ) { (sandpile[drop+1] <- sandpile[drop+1]+1)
if(sandpile[drop] > 1) {sandpile[drop] <- sandpile[drop]-2
```

But this does not continue a cascade, meaning the if sandpile[drop-1] > 1, sandpile[drop-2] and sandpile[drop] do not update.

So... My question is basically how I can implement the rest of the model, and then record the size of the avalanches (i.e. the amount of sand grains that move each update) for a number of n updates?