r - 将字符添加到条件 R 的现有单元格

标签 r character row

我有下表:

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/

相关文章:

r - R 中数据帧行上的按位 AND 或类似运算?

c++ - C++ char类findCharIndex

r - 考虑随机/混合效应后的共线性

r - 在R中将数据框的多列从字符串转换为数字

r - ggplot2 map 上的多个站点

c# - 如何从datagridview和数据库中删除选定的行

jquery - 如何以编程方式选择 JQGrid 的顶行?

java - 如何获取 JTextField 中的字符数?

java - 如何从java中的字符数组中获取替代字符?

pandas - 使用双键多索引在 Pandas DataFrame 中插入行失败