2017-05-11 4 views
1

rxDataStepを使用してxdfファイルに新しい列を追加したいとします。この目的を達成するために、私はこのコードを書かれている: xdfファイルに新しい列を追加する

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) 
} 

は、私はこのエラーを取得:別の変数の値に基づいてベクトル化変換のため

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 
+0

「datalist」はどのような構造ですか? –

+0

あなたは 'datalist'または' datallist'を意味しますか? –

+0

投稿を編集しました – Kaja

答えて

3

使用ifelse

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

rxDataStep(*, transformFunc=cashVsCard) 
+1

'transformFunc'を使わずに' ifelse'で 'transforms'パラメータを直接使うこともできます – Eqbal

+0

ありがとうございます。 'if..else..'文を' TransformFunc'と組み合わせてこのような関数に書くことは、一般的に可能ですか?これをチェックしてください(postst)/ postgres/questions/43975375/different-result-by- rxdatastep?noredirect = 1#comment74981314_43975375) – Kaja

関連する問題