I have a data frame structured as follows:

```
+----------+------+
| ID       | year |
+----------+------+
| 1        | 2002 |
| 1        | 2003 |
| 1        | 2004 |
| 2        | 2015 |
| 2        | 2016 |
| 2        | 2017 |
| 2        | 2018 |
| 3        | 2004 |
| 3        | 2005 |
+----------+------+

```

I would like to add a variable which flags the first (or earliest) occurrence within ID to get the following:

```
+----------+------+------+
| ID       | year | flag | 
+----------+------+------+
| 1        | 2002 | 1    |
| 1        | 2003 | 0    | 
| 1        | 2004 | 0    |
| 2        | 2015 | 1    |
| 2        | 2016 | 0    |
| 2        | 2017 | 0    |
| 2        | 2018 | 0    |
| 3        | 2004 | 1    | 
| 3        | 2005 | 0    |
+----------+------+------+

```

Is there an easy way to do this in dplyr?