I am working with R on cross-section data and having a problem when grouping the data under conditions. The problem can be seen clearly from a small part of my huge database as following. I would like to calculate the Average (Distance) under the conditions of same Province, District and Commune.

```
Province    District    Commune  Distance
101           15           3      15
101           15           3       5
101           15           3       7
101           15           9       1
101           15           9       7
102           18          19       3
102           18          19       10
103           16          22       5
103           16          22       6

```

The expected results would be the following (divided by each specific commune for each district and each province):

```
Province    District    Commune    Distance
101           15           3       average
101           15           9       average
102           18           19      average
103           16           22      average

```