我有下表:
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")
当停止点没有改变但 $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
(根据您想要的输出)
然后,对唯一索引进行操作以识别相同停靠点
的计数并返回到原始数据
关于r - 将字符添加到条件 R 的现有单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37002208/