【问题标题】:R partial string matching ignore spaces omni-directionalR部分字符串匹配忽略空格全方位
【发布时间】:2018-09-27 20:37:13
【问题描述】:

我遇到了部分字符串匹配的问题。我有成对的人,我需要比较他们的名字。为此,我在两个姓氏的两个方向上运行了一个charmatch,以查看name1 是否是name2 的一部分,反之亦然。我在下面有一个小数据集来演示这个问题。我在下面使用charmatch;我也使用过 pmatch 并且它返回相同的结果。

当 charmatch 说 seeks matches for the seeks matches for the seeks matches for the elements of its first argument between those of its second... 我认为这意味着它将 element1 中的每组字符视为模式 n 查看 element2 中是否存在相同的组。但这显然不是正在发生的事情,它看起来是特定于方向的。

所以……它是特定方向的吗?如果是这样……我还能用什么来做我所描述的事情?我的 EG 名称是双关语,我实际上遇到的是很多姓氏,其中丈夫有他的名字,妻子有她的 + 丈夫。我需要能够查看丈夫姓氏是否存在于妻子姓氏中。

我知道它可以用正则表达式来完成,但我不熟悉它们,可能应该是,但不是,所以我更喜欢不使用正则表达式的答案。

eg_data <- data.frame(name1 = c('Jimmy Conway', 'Jimmy'), 
name2 = c('Conway','Jimmy Conway'))

eg_data$share_name1 <- mapply(charmatch, eg_data$name1, eg_data$name2)
eg_data$share_name2 <- mapply(charmatch, eg_data$name2, eg_data$name1)
eg_data$share_name <- 0
eg_data$share_name [(eg_data$share_name1==1 | eg_data$share_name2==1)] 
<- 1

【问题讨论】:

    标签: r string pattern-matching string-matching


    【解决方案1】:

    同样的两行,只检测字符串,不检测字符匹配。

    eg_data$share_name1 <- mapply(str_detect,eg_data$name1, eg_data$name2)
    eg_data$share_name2 <- mapply(str_detect,eg_data$name2, eg_data$name1)
    

    甚至

    eg_data$share_name1 <- ifelse(mapply(str_detect,eg_data$name1, eg_data$name2)==TRUE,1,0)
    eg_data$share_name2 <- ifelse(mapply(str_detect,eg_data$name2, eg_data$name1)==TRUE,1,0)
    

    感谢任何看过的人。我希望这对其他人有所帮助。

    【讨论】:

      【解决方案2】:

      这可能很有用

      > with(eg_data, intersect(name1, name2))
      [1] "Jimmy Conway"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-19
        • 1970-01-01
        • 2015-09-14
        • 1970-01-01
        • 2014-05-18
        • 2016-10-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多