none
transforms vs. transformFunc in rxDataspet RRS feed

  • Question

  • Hi experts,

    I would like to generate a new column in my xdf file based on a column in this xdf file. I try both transforms and  transformFunc in rxDatastep.

    This code works fine for me:

    rxDataStep(nyc_jan_xdf,transforms = list(newCol5=ifelse(payment_type==1,10,20)))

    but if I do this task with  transformFunc :

    CashVsCard<-function(x)
    {
    
      if(x$payment_type==1)
      {
        x$newCol5=10
      }
      else
      {
        x$newCol5=20
      }
      return(x)
    }
    rxDataStep(nyc_jan_xdf,transformFunc = CashVsCard)


    I get this error:

    Error in doTryCatch(return(expr), name, parentenv, handler) : 
      The variable 'newCol5' has a different number of rows than other columns in the data: 1 vs. 10
    In addition: Warning message:
    In if (x$payment_type == 1) { :
      the condition has length > 1 and only the first element will be used

    Do you know why my transformFunc doesnt work?



    • Edited by Kohmare Monday, May 15, 2017 2:54 PM
    Monday, May 15, 2017 8:19 AM

Answers

  • I have got it.  although it is not the best solution but it works. I should only change the function like this:

    CashVsCard<-function(x)
    {
    
      p<-length(x$payment_type)   
      for(i in 1: p)
      {
    
        if(x$payment_type[i]==1)
        {
          x$cash_vs_Card4[i]="Card"
        }   else    {
          x$cash_vs_Card4[i]="Others"
        }
      }
      return(x)
    }

    • Marked as answer by Kohmare Tuesday, May 16, 2017 1:18 PM
    Tuesday, May 16, 2017 1:18 PM