Home > Mobile >  How to filter specific vector using matching condition in R
How to filter specific vector using matching condition in R

Time:12-04

I have two sets of Input vector. Either Policy Number or Contract Number comes as value for these inputs. But at a time only one will come.

I need to extract either of these Number and fit into policy_nr variable. The code works fine If I explicitly extract.

If I am trying to put OR operator and I am getting this Error

invalid 'x' type in 'x || y'

My Input Type 1

kk
 [1] "DUPLICATE x"                                                                                                                
 [2] "ifa’-"                                                                                                                      
 [3] "UPLICAT EXIDE Life"                                                                                                         
 [4] "Insurance"                                                                                                                  
 [5] "02-01-2020"                                                                                                                 
 [6] "Mr DEV WHITE T"                                                                                                       
 [7] "Contract Number : 123456"     

My Input Type 2

kk
 [1] "DUPLICATE x"                                                                                                                
 [2] "ifa’-"                                                                                                                      
 [3] "UPLICAT EXIDE Life"                                                                                                         
 [4] "Insurance"                                                                                                                  
 [5] "02-01-2020"                                                                                                                 
 [6] "Mr CRAIG WHITE T"                                                                                                       
 [7] "Policy Number : 7890"     

Code

policy_nr <- str_trim(sub(".*:", "", kk[grepl("Contract Number",kk)] ), side = c("both"))  || str_trim(sub(".*:", "", kk[grepl("Policy Number",kk)] ), side = c("both"))

CodePudding user response:

Just concatenate the character vector into one using paste, because the number might be at any element. You can encode the OR operator in a regular expression like this: "(Policy|Contract)"

library(tidyverse)

record1 <- c(
  "DUPLICATE x",
  "ifa’-",
  "UPLICAT EXIDE Life",
  "Insurance",
  "02-01-2020",
  "Mr DEV WHITE T",
  "Contract Number : 123456"
)

record2 <- c(
  "DUPLICATE x",
  "ifa’-",
  "UPLICAT EXIDE Life",
  "Insurance",
  "02-01-2020",
  "Mr CRAIG WHITE T",
  "Policy Number : 7890"
)

extract_number <- function(x) {
  x %>%
    paste0(collapse = " ") %>%
    str_extract("(Policy|Contract) Number : [0-9] ") %>%
    str_extract("[0-9] ") %>%
    as.numeric()
}


extract_number(record1)
#> [1] 123456
extract_number(record2)
#> [1] 7890

Created on 2021-12-03 by the enter image description here

  •  Tags:  
  • r
  • Related