比较字符并返回R中的不匹配项

时间:2019-03-21 15:47:58

标签: r compare

我想迭代比较字符并返回数据帧2列之间的不匹配。

如果x2x,y67y不返回,因为x仍然是x,y仍然是y。

输入:

x y    x_val              y_val
A  B   x2x, y67h, d7j  x2y, y67y, d7r
B  C   x2y, y67y, d7r  x2y, y67y, d7r
C  A   x2y, y67y, d7r  x2x, y67h, d7j  
C  D   x2y, y67y, d7r  x67b, g72v, b8c
D  E   x67b, g72v, b8c  x67r, g72j

我想添加一列val并返回x_val和y_val之间的差异

输出:

x y       x_val             y_val           val
A  B   x2x, y67h, d7j  x2y, y67y, d7r     x2y, d7r
B  C   x2y, y67y, d7r  x2y, y67y, d7r     NA
C  A   x2y, y67y, d7r  x2x, y67h, d7j     y67h, d7j
C  D   x2y, y67y, d7r  y67b, g72v, b8c    y67b, g72v, b8c
D  E   y67b, g72v, b8c  y67b, g72j        g72j

我尝试了xy_val <- y_val[!(y_val %in% x_val)]

能否请您提出有关输出不匹配项的解决方案。

我的数据:

structure(list(x = c("A", "B", "C", "C", "D"), y = c("B", "C", "A", "D", "E"), x_val = c("x2x, y67h, d7j", "x2y, y67y, d7r", "x2y, y67y, d7r", "x2y, y67y, d7r", "y67b, g72v, b8c"), y_val = c("x2y, y67y, d7r", "x2y, y67y, d7r", "x2x, y67h, d7j", "y67b, g72v, b8c", "y67b, g72j" )), class = "data.frame", row.names = c(NA, -5L))

感谢您的帮助!

谢谢

2 个答案:

答案 0 :(得分:0)

这能达到预期的结果吗?

check_this = function(temp_data)
{
  print(temp_data)

  string_1 = gsub(", ", " ",   temp_data["x_val"])
  string_2 = gsub(", ", " ",   temp_data["y_val"])

  string_sub_1 = gsub(" ", "|", string_1)
  string_sub_2 = gsub(" ", "|", string_2)

  unmatche_s1 = gsub(string_sub_2, "", string_1)
  unmatche_s2 = gsub(string_sub_1, "", string_2)

  # return both as a list - if you need only unmachtedy_in_x you can just return(unmatched_s2)
  return(list(unmatchedx_in_y = unmatche_s1, unmatchedy_in_x = unmatche_s2))

}

res = apply(f, 1, check_this)

答案 1 :(得分:0)

使用dplyrpurrr

library(dplyr)
library(purrr)

f %>% mutate(diff_x = map2_chr(strsplit(x_val, split = ", "), 
                               strsplit(y_val, split = ", "), 
                               ~paste(grep('([a-z])(?>\\d+)(?!\\1)', setdiff(.x, .y), 
                                           value = TRUE, perl = TRUE), 
                                           collapse = ", ")) %>%
               replace(. == "", NA), 
             diff_y = map2_chr(strsplit(x_val, split = ", "), 
                               strsplit(y_val, split = ", "), 
                               ~paste(grep('([a-z])(?>\\d+)(?!\\1)', setdiff(.y, .x), 
                                           value = TRUE, perl = TRUE),
                                           collapse = ", ")) %>%
               replace(. == "", NA))

注释:

  1. grep接受setdiff的输出,并删除任何格式为“相同字符,中间有数字”的元素。

  2. ([a-z])匹配任何字母字符。

  3. (?>\\d+)是一个原子组,可匹配任何长度的数字,但不会回溯。

  4. (?!\\1)是否定的前瞻,与([a-z])

  5. 匹配的内容匹配

输出:

  x y           x_val           y_val    diff_x          diff_y
1 A B  x2x, y67h, d7j  x2y, y67y, d7r y67h, d7j        x2y, d7r
2 B C  x2y, y67y, d7r  x2y, y67y, d7r      <NA>            <NA>
3 C A  x2y, y67y, d7r  x2x, y67h, d7j  x2y, d7r       y67h, d7j
4 C D  x2y, y67y, d7r y67b, g72v, b8c  x2y, d7r y67b, g72v, b8c
5 D E y67b, g72v, b8c      y67b, g72j g72v, b8c            g72j