Question: I have a table:

```
  id <- c(1,1,2,2,2,2,2,3,3,4,4,5,5,5)
  dist <- c(0,1,1,0,2,15,0,4,4,0,5,5,16,2)
  data <- data.frame(id, dist )

```

I would like to edit the column `id` when dist is superior to a certain value (let´s say 10). I am looking to add `+1` when `data$dist >10`

The final output would be:

```
  data$id_new <-  c(1,1,2,2,2,3,3,4,4,5,5,6,7,7)

```

Is it possible to do something with a `if` loop? I tried to something with a loop but I am still not successful.

Answer: 
Maybe using cumsum:

```
data$new_id <- data$id + cumsum(data$dist > 10)

```

**Explanation:**

`cumsum(data$dist > 10)` will return the cumulative sum of indices in `data$dist` which are greater than 10. You can see how this works by taking the expression apart in R and seeing how each piece works.
We can use `duplicated` with `>`

```
with(data, cumsum(dist > 10| !duplicated(id)))
#[1] 1 1 2 2 2 3 3 4 4 5 5 6 7 7

```