如何防止ifelse语句清除以前的数据?

时间:2019-05-17 17:46:14

标签: r if-statement

我正在尝试将一些会计数据导入新软件,并且需要添加借方列journalItemLine_debitAmount和贷方列journalItemLine_creditAmount,如果来源中存在借方或贷方,这些列将被填充数据。运行脚本时,以正数表示的数据仅以贷方结尾,以正数表示的则不以借方结尾。

data = within(data, {
journalItemLine_debitAmount = ifelse(If.Positive. == "Debit" & Amount>=0, Amount, "")
journalItemLine_creditAmount = ifelse(If.Positive. == "Debit" & Amount<0, -Amount, "")
journalItemLine_debitAmount = ifelse(If.Positive. == "Credit" & Amount<0, -Amount, "")
journalItemLine_creditAmount = ifelse(If.Positive. == "Credit" & Amount>=0, Amount, "")
 })

以下是源数据:

Amount     If.Positive.
0.00     Debit
-546     Debit
789     Credit
45789     Debit
-34657     Credit

这就是我想要的:

Amount     If.Positive.     journalItemLine_debitAmount     journalItemLine_creditAmount
0.00     Debit     0
-54623     Debit          54623
78934     Credit          78934
45789     Debit     45789
-34657     Credit     34657

不确定我是否格式化了我刚刚放入的数据,但可以想象每个数据都是一列。

我也尝试了这个if语句,但是似乎什么也没发生。

journalItemLine_debitAmount = if((If.Positive. == "Debit") && (Amount>=0)){Amount}
journalItemLine_creditAmount = if((If.Positive. == "Debit") && (Amount<0)){-Amount}
journalItemLine_debitAmount = if((If.Positive. == "Credit") && (Amount<0)){-Amount}
journalItemLine_creditAmount = if((If.Positive. == "Credit") && (Amount>=0)){Amount}
})

2 个答案:

答案 0 :(得分:0)

我想你只是想要

data = within(data, {
  journalItemLine_debitAmount = ifelse(
    (If.Positive. == "Debit" & Amount>=0) | (If.Positive. == "Credit" & Amount<0),
     abs(Amount), NA)
  journalItemLine_creditAmount = ifelse(
     (If.Positive. == "Credit" & Amount>=0) | (If.Positive. == "Debit" & Amount<0),
     abs(Amount), NA)
})

这避免了两次将相同的值重新赋值,而只是使用绝对值来保持数字为正。

答案 1 :(得分:0)

您也可以尝试:

public void onPageFinished(WebView view, String url) {
  String GetSomething  = "javascript:GetSomething()"; // GetSomething returns a Promise
  webView.evaluateJavascript(GetLocationListing, new ValueCallback<String>() {
      @Override
      public void onReceiveValue(String s) {
        Log.d("CONSOLE RECEIVED THE", s); // Null
      }
  });
}

或更简单:

data$journalItemLine_debitAmount[If.Positive. == "Debit" & Amount>=0]<- Amount
data$journalItemLine_debitAmount[If.Positive. == "Debit" & Amount<0] <- -Amount
data$journalItemLine_creditAmount[If.Positive. == "Credit" & Amount<0] <- -Amount
data$journalItemLine_creditAmount[If.Positive. == "Credit" & Amount>=0] <- Amount

希望有帮助!