【问题标题】:subset dataframe where two digits are interchanged in R在 R 中交换两位数字的子集数据帧
【发布时间】:2018-07-23 14:44:09
【问题描述】:

我在下面提到了数据帧:

df <- read.table(text =
"code        Num        mail           identifier      U_id
YY-12       12345      jjf@gmail.com  ar145j          U-111
YY-13       12345      jjf@gmail.com  Ra145J          U-111
YY-14       48654      ert@gmail.com  at188R          U-112
YY-15       48654      Ert@gmail.com  At819R          U-113
YY-16       88994      fty@ymail.com  fr789U          U-114
YY-17       88994      fty@ymail.com  Rf789X          U-115
YY-18       14500      foi@ymail.com  xr747Y          U-116
YY-19       14500      foi@ymail.com  xY747C          U-117", header = T)

利用上述数据框,我想获取那些行的子集,其中对于相同的 Nummail,我们有不同的标识符,连续 2 位数的差异。

例如在下面提到的输出中,标识符ar145j 更改为Ra145J

所需输出:

code        Num        mail           identifier      U_id
YY-12       12345      jjf@gmail.com  ar145j          U-111
YY-13       12345      jjf@gmail.com  Ra145J          U-111
YY-14       48654      ert@gmail.com  at188R          U-112
YY-15       48654      Ert@gmail.com  At819R          U-113

【问题讨论】:

    标签: r stringdist


    【解决方案1】:

    这可能会有所帮助

    library(tidyverse)
    library(stringi)
    df %>%
      group_by(Num, mail) %>%
      filter(n() == 1 | toupper(first(substr(identifier, 1, 2))) == 
               stri_reverse(toupper(last(substr(identifier, 1, 2)))))
    # A tibble: 6 x 5
    # Groups:   Num, mail [4]
    #  code    Num mail          identifier U_id 
    #  <fct> <int> <fct>         <fct>      <fct>
    #1 YY-12 12345 jjf@gmail.com ar145j     U-111
    #2 YY-13 12345 jjf@gmail.com Ra145J     U-111
    #3 YY-14 48654 ert@gmail.com at188R     U-112
    #4 YY-15 48654 Ert@gmail.com At819R     U-113
    #5 YY-16 88994 fty@ymail.com fr789U     U-114
    #6 YY-17 88994 fty@ymail.com Rf789X     U-115
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-08
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多