我有一个数据框,其中包含关于比赛的信息,其中一列显示了在特定时间点驾驶汽车行驶的距离。它看起来像这样:
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
如果我知道一圈的距离是5个单位,我想创建一个新列,根据驾驶的距离确定每个数据点所采取的圈数。结果应如下所示:
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
我该怎么做,最好使用tidyverse?
5
,应该是 1 还是 2? - Onyambu