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?
CodePudding user response:
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
CodePudding user response:
Another option is to use group_by()
, case_when()
and between()
.
Code:
df_desired = df %>%
group_by(id, distance,
lap = case_when(between(distance, 1, 5)~"1",
between(distance, 5, 10)~ "2",
between(distance, 10, 15)~"3",
between(distance, 15, 20)~"4"))
# Desired output
# A tibble: 15 × 3
# Groups: id, distance, lap [15]
id distance lap
<chr> <dbl> <chr>
1 A 1 1
2 A 2.36 1
3 A 3.71 1
4 A 5.07 2
5 A 6.43 2
6 A 7.79 2
7 A 9.14 2
8 A 10.5 3
9 A 11.9 3
10 A 13.2 3
11 A 14.6 3
12 A 15.9 4
13 A 17.3 4
14 A 18.6 4
15 A 20 4