-3

我有一个 dtl 表和一个查找表(GLMap)

分:

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    

查找表(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 电源 S 10 MAIN 6712

我想创建一个输出表,它是 dtl 表的副本,但根据查找表中多个列的匹配添加了一个额外的 GLACCT 列。

逻辑是:如果 GLAccount 列为空,则从查找表 (GLacctMap) 中获取匹配的帐户,否则将 GLAccount 编号放入新列中。

这是我尝试的一个功能:

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)

我收到以下错误:IBCO Spotfire Statistics Services 返回了一个错误:'as.data.frame.default(passed.args[[i]], stringsAsFactors = s 中的错误:无法将类 '"function"' 强制转换为数据。框架'。

4

1 回答 1

0

你没有说你期待什么。如果您想要逐行应用该函数并且您的列之一被命名为“GLAccout”,那么您可以这样做:

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

或者:

temptb$GLACCT <- apply(temptb, 1,  findgl )

但是,将函数逐列应用(这就是“2”将要执行的操作)到命名列是没有意义的。但是为了使这种策略成功,您需要进行一些测试并使用一些实际数据,我将从这个完全未经测试的修改开始:

findgl <- function(x){ 
        if( is.na(x$GLAccount)  ) {   # no way to be length 0 or null
                 GLACCT <- GLacctMap[ match( paste( dld$COMPANY_CODE, dld$DLT_DEAL_TYPE, dld$DN_DIRECTION, sep=":"), 
                                             paste(GLacctMap$DealType,GLacctMap$DR_CR,sep=":")), "GLAccount"]    
             } else{
                GLACCT <- x$GLAccount
             }    
       GLACCT 
        }
于 2016-11-19T00:13:38.217 回答