1

I would like to add a new column to an xdf file using rxDataStep. To achieve this aim, I have written this code:

    rxDataStep(nyc_jan_xdf,transformFunc = CashVsCard )

CashVsCard<-function(dataList)
{

  if(dataList$payment_type==1)
  {
    dataList$cash_vs_Card="Card"
  }
  else
  {
    if(dataList$payment_type==2)
    {
      dataList$cash_vs_Card="Cash"
    }
  }

  return(dataList)
}

then I get this error:

   The variable 'cash_vs_Card' has a different number of rows than other columns in the data: 1 vs. 10
Caught exception in file: CxAnalysis.cpp, line: 3830. ThreadID: 7288 Rethrowing.
Caught exception in file: CxAnalysis.cpp, line: 5347. ThreadID: 7288 Rethrowing.
Error in doTryCatch(return(expr), name, parentenv, handler) : 
  The variable 'cash_vs_Card' has a different number of rows than other columns in the data: 1 vs. 10
In addition: Warning messages:
1: In if (dataList$payment_type == 1) { :
  the condition has length > 1 and only the first element will be used
2: In if (dataList$payment_type == 2) { :
  the condition has length > 1 and only the first element will be used
Kaja
  • 2,962
  • 18
  • 63
  • 99

1 Answers1

3

Use ifelse for vectorised transformations based on the values of another variable.

cashVsCard <- function(datalist)
{
      datalist$cash_vs_card <- ifelse(datalist$payment_type == 1, "Card", "Cash")
      datalist
}

rxDataStep(*, transformFunc=cashVsCard)
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
  • 1
    You can also use `transforms` parameter directly with `ifelse` without using `transformFunc` – Eqbal May 11 '17 at 18:01
  • Thank you for your answer. Is it generally possible to write ` if..else..`statement in such a function in combination with `TransformFunc` please check this [post](http://stackoverflow.com/questions/43975375/different-result-by-transforms-and-transformfunc-in-rxdatastep?noredirect=1#comment74981314_43975375) – Kaja May 15 '17 at 09:34