Error with apply function

I have a dtl table and a lookup table (GLMap)

dtl:

DEAL_TYPE   DN_DIRECTION    key COMPANY_CODE    GLAccount
POWER   SALE        1   AFFL_CO 1702
POWER   PURCHASE    2   AFFL_CO 3702
MISC    SALE        3   AFFL_CO 5717
MISC    PURCHASE    4   AFFL_CO 5718
POWER   SALE        5   AFFL_CO 1702
POWER   SALE        6   MAIN    
POWER   PURCHASE    7   MAIN    
MISC    SALE        8   MAIN    
MISC    PURCHASE    9   MAIN    
POWER   SALE        10  MAIN    

Lookup Table (GLAcctMap):

DealType    DR_CR   GLAccount   Customer
POWER   P   3702    AFFL_CO
POWER   S   1702    AFFL_CO
MISC    P   5718    AFFL_CO
MISC    S   5717    AFFL_CO
POWER   P   6702    STD
POWER   S   6712    STD
MISC    P   5312    STD
MISC    S   5313    STD

Expected Output: DEAL_TYPE DN_DIRECTION key COMPANY_CODE GLAccount GLACCT POWER S 1 AFFL_CO 1702 1702 POWER P 2 AFFL_CO 3702 3702 MISC S 3 AFFL_CO 5717 5717 MISC P 4 AFFL_CO 5718 5718 POWER S 5 AFFL_CO 1702 1702 POWER S 6 MAIN 6712 POWER P 7 MAIN 6702 MISC S 8 MAIN 5313 MISC P 9 MAIN 5312 POWER S 10 MAIN 6712

I would like to create an Output table which is a copy of the dtl table but with an extra GLACCT column added based on a match on multiple columns from the lookup table.

Logic is: if GLAccount column is null then go get the matching Account from the lookup table (GLacctMap), else put the GLAccount number in the new column.

Here’s a function I tried:

load_details <- function(dld) { 

phys_fin = 0

 findgl <- function(x){ 
    if( is.na(x$GLAccount)  ) {   
             GLACCT <- GLacctMap[ match( paste(x$DEAL_TYPE, x$DN_DIRECTION, sep=":"), 
                                         paste( GLacctMap$DealType,GLacctMap$DR_CR,sep=":")), "GLAccount"]    
         } else {

              GLACCT <- x$GLAccount
         }    
   GLACCT 
    }  

dld$GLACCT <- apply(currdld, 1, function(x) findgl )

}

PEDLD <- load_details(currdld)

I get the following Error: IBCO Spotfire Statistics Services returned an error: ‘Error in as.data.frame.default(passed.args[[i]], stringsAsFactors = s : cannot coerce class ‘”function”‘ into a data.frame’.

Leave a Comment