<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />大家好,
这个问题,我多次来过;
将一种类型的公式应用于许多新生成的列。有人可以解释一下它背后的主要逻辑吗?
例如:
1。我的数据集为72年,有12个月。我想将年/月中的一个设置为最佳并接受为100 =我想将所有其他年份与该年/月的百分比进行比较(特别是收入增加或减少百分比)。
2。我有1列认为MainColumn在数据表中有100个其他列(1,2,3,4,5,...),并且添加了100个新列我想为每个列使用相同的公式:NewColumn1 = Column1 / MainColumn,NewColumn2 = Column2 / MainColumn ......等等......
3。我在配对关系中有列(Column1 Column2,Column3 Column4),并且想要创建新列(ColumnNew1),其中包含与配对列相关的公式(= Column1 + Column2),(ColumnNew2 = Column3 + Column4)...依此类推。
怎么做?
非常感谢
我假设您正在寻找脚本解决方案。以下是一些想法。
//Example tables
dt1 = New Table("example 1",
add rows(12 * 72),
New Column("year", numeric, Formula(Sequence(1944, 2015, 1, 12))),
New Column("month", numeric, values(Repeat(1 :: 12, 72))),
New Column("revenue", numeric, Formula(Random Integer(10000)))
);
dt2 = New Table("example 2", add rows(5), New Column("main column", numeric, values(1 :: 5)));
dt2 << add multiple columns("column", 10, after last, numeric);
For(i = 2, i <= 11, i++,
Column(dt2, i) << set formula(Random Integer(10))
);
// 1)
dt1 << New Column("revenue, % of year's best month", numeric, Format("percent"), Formula(:revenue / Col Max(:revenue, year)));
// 2) and 3) The trick is to use Eval Expr() and Name Expr() when defining formulas.
cols = dt2 << get column names;
// add formulas for 2
For(i = 2, i <= 11, i++,
colname = Char(cols[i]) || " by main";
f = Eval Expr(Expr(cols[i]) / :main column);
dt2 << New Column(colname, numeric, formula(Name Expr(f)));
);
// dito for 3
For(i = 2, i <= 6, i = i + 2,
colname = Char(cols[i]) || "+" || Char(cols[i + 1]);
f = Eval Expr(Expr(cols[i]) + Expr(cols[i + 1]));
dt2 << New Column(colname, numeric, formula(Name Expr(f)));
);