rluhn

Is there any way to run a vector in checkluhn package?


I try to run the following code:

library(checkLuhn)
library(gsheet)
data<-gsheet2tbl("https://docs.google.com/spreadsheets/d/145Wowgp6NXmcj-IqqKfZ-2B2aKwAOzZ1VuCjxAq7WeM/edit?usp=sharing")


df <- data.frame(Card  = character(), 
                     Active = character(),
                     issuer =character())

for (i in 1: nrow(data))
{
  card <- data[i,2]  
  active <- checkLuhn(card)
  issuer <- issuer(card)
  df = rbind(df, data.frame(Card = card, Active = active, Issuer =issuer, stringsAsFactors = FALSE))
}

But it is showing the following error:

Error in data.frame(Card = card, Active = active, Issuer = issuer, stringsAsFactors = FALSE) : 
  arguments imply differing number of rows: 1, 0

Can anyone help me please?


Solution

  • The issue seems to be with issuer which returns logical FALSE if there is none or else return a two column tibble. We could check the output type and make changes

    library(purrr)
    library(checkLuhn)
    library(gsheet)
    map_dfr(data$Number, ~  {
      active <- checkLuhn(.x)
      issuer <- issuer(.x)['issuer']
      if(is.logical(issuer)) issuer <- tibble(issuer= NA_character_)
       tibble(card = .x, active, issuer)
      })
    

    -output

    # A tibble: 18 × 3
       card                active issuer                   
       <chr>               <lgl>  <chr>                    
     1 378 2822 4631 0005  TRUE   American Express         
     2 371 4496 3539 8431  TRUE   American Express         
     3 378 7344 9367 1000  TRUE   American Express         
     4 5610 5910 8101 8250 TRUE   Bankcard                 
     5 5610 5910 8101 8250 TRUE   Maestro                  
     6 30 5693 0902 5904   TRUE   Diners Club Carte Blanche
     7 6011 1111 1111 1110 FALSE  <NA>                     
     8 6011 0009 9013 9420 FALSE  <NA>                     
     9 3530 1113 3330 0000 TRUE   JCB                      
    10 3566 0020 2036 0500 FALSE  <NA>                     
    11 5555 5555 5555 4440 FALSE  <NA>                     
    12 5105 1051 0510 5100 TRUE   MasterCard               
    13 4111 1111 1111 1110 FALSE  <NA>                     
    14 4012 8888 8888 1880 FALSE  <NA>                     
    15 4 2222 2222 2222    TRUE   Visa                     
    16 760 0924 4561       FALSE  <NA>                     
    17 5019 7170 1010 3740 FALSE  <NA>                     
    18 6331 1019 9999 0010 FALSE  <NA>          
    

    Or using the OP's code

    df <- data.frame(Card  = character(), 
                         Active = character(),
                         Issuer =character())
    for (i in 1:nrow(data))
    {
      card <- data[[2]][i]  
      active <- checkLuhn(card)
      issuer <- issuer(card); 
      if(NROW(issuer) == 0 |is.logical(issuer))  {
         issuer <-  NA_character_
        }else {
        issuer <- issuer$issuer    }
      df <- rbind(df, data.frame(Card = card, 
         Active = active, Issuer =issuer, stringsAsFactors = FALSE))
    }
    

    -output

     df
                      Card Active                    Issuer
    1   378 2822 4631 0005   TRUE          American Express
    2   371 4496 3539 8431   TRUE          American Express
    3   378 7344 9367 1000   TRUE          American Express
    4  5610 5910 8101 8250   TRUE                  Bankcard
    5  5610 5910 8101 8250   TRUE                   Maestro
    6    30 5693 0902 5904   TRUE Diners Club Carte Blanche
    7    38 5200 0002 3237   TRUE                      <NA>
    8  6011 1111 1111 1110  FALSE                      <NA>
    9  6011 0009 9013 9420  FALSE                      <NA>
    10 3530 1113 3330 0000   TRUE                       JCB
    11 3566 0020 2036 0500  FALSE                      <NA>
    12 5555 5555 5555 4440  FALSE                      <NA>
    13 5105 1051 0510 5100   TRUE                MasterCard
    14 4111 1111 1111 1110  FALSE                      <NA>
    15 4012 8888 8888 1880  FALSE                      <NA>
    16    4 2222 2222 2222   TRUE                      Visa
    17       760 0924 4561  FALSE                      <NA>
    18 5019 7170 1010 3740  FALSE                      <NA>
    19 6331 1019 9999 0010  FALSE                      <NA>