我有下表:

structure(list(Id = structure(c(1L, 1L, 2L, 2L, 1L, 3L, 3L, 3L
), .Label = c("a", "b", "c"), class = "factor"), stops = c(1,
1, 1, 1, 1, 2, 2, 2)), .Names = c("Id", "stops"), row.names = c(NA,
-8L), class = "data.frame")

我想在$ stops不变但$ Id不变的情况下向$ stops添加新字符。

例如,我想得到:
structure(list(Id = structure(c(1L, 1L, 2L, 2L, 1L, 3L, 3L, 3L
), .Label = c("a", "b", "c"), class = "factor"), stops = structure(c(1L,
1L, 2L, 2L, 3L, 4L, 4L, 4L), .Label = c("1", "1-1", "1-2", "2"
), class = "factor")), .Names = c("Id", "stops"), row.names = c(NA,
-8L), class = "data.frame")

如果ID与上一个不同,并且Stops与上一个相同,我只是想这样做。

我尝试使用mutate(),但似乎很遥远,可以在这里工作...

最佳答案

这是使用data.table的循环尝试

library(data.table)
setDT(df)[, `:=`(stops = as.character(stops), Idindx = rleid(Id))]
indx <- unique(df, by = "Idindx")[, counter := (1:.N) - 1L, by = rleid(stops)]
df[indx[counter > 0], stops := paste(stops, i.counter, sep = "-"), on = "Idindx"]
#    Id stops Idindx
# 1:  a     1      1
# 2:  a     1      1
# 3:  b   1-1      2
# 4:  b   1-1      2
# 5:  a   1-2      3
# 6:  c     2      4
# 7:  c     2      4
# 8:  c     2      4

第一步是为每个Id创建一个唯一索引(因为它们不是唯一的),并将stops转换为character(根据您所需的输出)

然后,对唯一索引进行操作以标识相同stops的计数并重新加入原始数据

09-15 22:12