我有下面的dataframe,其中每一行代表文本的变化。然后,我使用adist()
函数提取更改是匹配(M)、插入(I)、替换(S)还是删除(D)。
我需要在change
列中找到I
s的所有索引(在insrtion_idx
列中显示)。使用这些索引,我需要提取current_text
中的相应字符(在这里以insertion_chars
为例)。
df <- tibble(current_text = c("A","AB","ABCD","ABZ"),
previous_text = c("","A","AB","ABCD"),
change = c("I","MI","MMII","MMSD"),
insertion_idx = c(c(1),c(2),c(3,4),""),
insertion_chars = c("A","B","CD",""))
我尝试过拆分字符串并比较字符串的差异,但对于真实世界的数据,这会变得非常混乱。如何完成上述任务?
2条答案
按热度按时间p3rjfoxz1#
把我关于使用
gregexpr
和regmatches
的评论变成一个答案。如果您正在寻找替代方法,此过程中的许多内容与此问题中的内容非常相似-Extract a regular expression match。
u2nhd7ah2#
尝试以下替代thelatemail的(优秀的)推荐(同样有效):
请注意,
insertion_idx
是一个列表列,其中包含您要查找的索引: