【问题标题】:R - calculate column based on formula stored in other columnR - 根据存储在其他列中的公式计算列
【发布时间】:2018-08-17 16:18:33
【问题描述】:

我的表包含数据rebate calculation

在R中,如何添加一个新的列“rebate”,它的值是基于“formula”列的计算?

以下代码有效,但结果错误。

test_df <- data.frame(
  din_pin = c(12345678, 23456789, 2789),
  eff_price = c(10, 6, 0.6),
  qty = c(100, 100, 1000),
  list_price = c(12, 7, 0.85),
  form_price = c(0, 5.5, 0.65),
  formula = c("(eff_price - list_price)*qty", 
              "(form_price - list_price)*qty", 
              "(eff_price - list_price)*qty")
)

for (row in 1:nrow(test_df)){
  formula_text <- as.character(test_df[row, "formula"])
  print(formula_text)
  test_df$rebate[row] <- eval(parse(text = formula_text), test_df)
}

如果我将公式值更改为:

test_df <- data.frame(
  din_pin = c(12345678, 23456789, 2789),
  eff_price = c(10, 6, 0.6),
  qty = c(100, 100, 1000),
  list_price = c(12, 7, 0.85),
  form_price = c(0, 5.5, 0.65),
  formula = c("(test_df$eff_price[row] - test_df$list_price[row])*test_df$qty[row]", 
              "(test_df$form_price[row] - test_df$list_price[row])*test_df$qty[row]", 
              "(test_df$eff_price[row] - test_df$list_price[row])*test_df$qty[row]")
)

结果是正确的。

但是,我希望代码是通用的(独立于数据框)。

我在 SAS 中完成了类似的任务:

data test;
    infile datalines dsd dlm = "," missover;

    input din_pin       :8.
          eff_price     :9.4
          qty           :8.
          list_price    :9.4
          former_price  :9.4
          formula       :$50.
    ;

datalines;
12345678, 10.0000, 100, 12.0000, 0.0000, (eff_price - list_price)*qty
23456789, 6.0000, 100, 7.0000, 5.5000, (former_price - list_price)*qty
2789, 0.60000, 1000, 0.850000, 0.6500, (eff_price - list_price)*qty
;
run;

data _null_;
    set test end=end;
    count+1;
    call symputx('rebate_formula'||left(count),compress(formula));
    if end then call symputx('max',count);
run;

%macro calculate_rebate;
    data rebate;
        set test;
        %do i = 1 %to &max;
            if _n_ = &i then do;
                rebate = &&rebate_formula&i;
            end;
        %end;
    run;
%mend calculate_rebate;

%calculate_rebate;

proc print data = rebate;
run;

我认为在 R 中它应该更容易。我就是搞不定。

【问题讨论】:

  • R 以函数式编程为中心。通常不会将代码命令保存在数据结构中,而是通过各种需要的逻辑显式运行代码,这有助于调试和工作流。

标签: r sas


【解决方案1】:

问题是 eval 不知道您正在评估哪一行。尝试将其更改为:

test_df$rebate[row] <- eval(parse(text = formula_text), test_df[row,])

【讨论】:

  • 不客气。如果这回答了您的问题,请接受并投票。
【解决方案2】:

感谢 DomPazz。

这是修改后的代码:

test_df <- data.frame(
  din_pin = c(12345678, 23456789, 2789),
  eff_price = c(10, 6, 0.6),
  qty = c(100, 100, 1000),
  list_price = c(12, 7, 0.85),
  form_price = c(0, 5.5, 0.65),
  formula = c("(eff_price - list_price)*qty", 
              "(form_price - list_price)*qty", 
              "(eff_price - list_price)*qty")
)

for (row in 1:nrow(test_df)){
  formula_text <- as.character(test_df[row, "formula"])
  print(formula_text)
  test_df$rebate[row] <- eval(parse(text = formula_text), test_df[row,])
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 2020-11-16
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 2014-11-04
    相关资源
    最近更新 更多