R: count laps based on distance column

I have a dataframe with information about a race and one column shows the distance that was driven by a car at a specific timepoint. It looks something like this:

data.frame(id = rep(c("A"), each = 15), 
           distance = seq(from = 1, to = 20, length.out = 15))

   id  distance 
1   A  1.000000   
2   A  2.357143   
3   A  3.714286   
4   A  5.071429   
5   A  6.428571   
6   A  7.785714   
7   A  9.142857   
8   A 10.500000   
9   A 11.857143   
10  A 13.214286   
11  A 14.571429   
12  A 15.928571   
13  A 17.285714   
14  A 18.642857   
15  A 20.000000   

If I know that one lap is 5 units, I would like to create a new column that tells the lap at which each data point was taken, based on the distance driven. The result should look like this:

data.frame(id = rep("A", each = 15), 
           distance = seq(from = 1, to = 20, length.out = 15), 
           lap = c(1,1,1,2,2,2,2,3,3,3,3,4,4,4,4))

   id  distance lap
1   A  1.000000   1
2   A  2.357143   1
3   A  3.714286   1
4   A  5.071429   2
5   A  6.428571   2
6   A  7.785714   2
7   A  9.142857   2
8   A 10.500000   3
9   A 11.857143   3
10  A 13.214286   3
11  A 14.571429   3
12  A 15.928571   4
13  A 17.285714   4
14  A 18.642857   4
15  A 20.000000   4

How can I do this, preferably using tidyverse?

>Solution :

This is an integer division problem. Just divide the distance by 5, and take the ceiling, which rounds it up to the nearest integer. This will give you the current lap number:

dplyr::mutate(df, lap = ceiling(distance/5))

   id  distance lap
1   A  1.000000   1
2   A  2.357143   1
3   A  3.714286   1
4   A  5.071429   2
5   A  6.428571   2
6   A  7.785714   2
7   A  9.142857   2
8   A 10.500000   3
9   A 11.857143   3
10  A 13.214286   3
11  A 14.571429   3
12  A 15.928571   4
13  A 17.285714   4
14  A 18.642857   4
15  A 20.000000   4

Leave a Reply