0

I have below mentioned datafram:

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)

Utilizing the above mentioned dataframe, I want to get subset of those rows where for the same Num and mail, we have different identifier with consecutive 2 digits difference.

For example in the below mentioned output, identifier ar145j changed to Ra145J.

Required Output:

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
Jupiter
  • 221
  • 1
  • 12

1 Answers1

0

May be this would help

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
akrun
  • 874,273
  • 37
  • 540
  • 662