我如何找到相似字符串之间的差异?

问题描述 投票:0回答:1

我有一个字符串向量(准确的文件名)。

pav <- c("Sn_4Khz_3W_45_130_02_30cm_101mm_",
         "Sn_4Khz_4W_45_130_02_30cm_101mm_",
         "Sn_4Khz_4W_50_130_02_30cm_101mm_")

我正在寻找一种简单的方法来查找这些字符串之间的差异。

`> char_position_fun(pav) # gives unique character position
[1] 9 12 13 `


`> char_diff_fun(pav) # removes matching components (position and value)
[1] 3_4_5  4_4_5  4_5_0`
r string string-comparison
1个回答
0
投票
如果您希望获得所描述的结果,则可以添加更多操作。

strsplit(mytext, split = "") %>% map_dfr(.x = ., .f = function(x) enframe(x, name = "position", value = "word"), .id = "id") %>% group_by(position) %>% mutate(check = n_distinct(word) == 1) %>% filter(check == FALSE) %>% group_by(id) %>% summarize_at(vars(position:word), .funs = list(~paste0(., collapse = "_"))) id position word <chr> <chr> <chr> 1 1 9_12_13 3_4_5 2 2 9_12_13 4_4_5 3 3 9_12_13 4_5_0

DATA

mytext <- c("Sn_4Khz_3W_45_130_02_30cm_101mm_", "Sn_4Khz_4W_45_130_02_30cm_101mm_", 
"Sn_4Khz_4W_50_130_02_30cm_101mm_")
© www.soinside.com 2019 - 2024. All rights reserved.