如何将两个表相乘我有一个大数据表和一个归一化因子表。
数据表如下所示:
| ID | 类型 | 参数1 | 参数2 | 参数3 |
| 01 | 1个 | 数据 | 数据 | 数据 |
| 02 | 1个 | 数据 | 数据 | 数据 |
| 03 | 2个 | 数据 | 数据 | 数据 |
| 04 | 2个 | 数据 | 数据 | 数据 |
归一化因子表如下所示:
| 类型 | 参数1 | 参数2 | 参数3 |
| 1个 | 归一化因子1 | 归一化因子2 | 归一化因子3 |
| 2个 | 归一化因子4 | 归一化因子5 | 归一化因子6 |
如何根据类型和参数将第一个表乘以第二个表,并获得一个新表,如下所示:
| ID | 类型 | 参数1 | 参数2 | 参数3 |
| 01 | 1个 | 数据*归一化因子1 | 数据*归一化因子2 | 数据*归一化因子3 |
| 02 | 1个 | 数据*归一化因子1 | 数据*归一化因子2 | 数据*归一化因子3 |
| 03 | 2个 | 数据*归一化因子4 | 数据*归一化因子5 | 数据*归一化因子6 |
| 04 | 2个 | 数据*归一化因子4 | 数据*归一化因子5 | 数据*归一化因子6 |
我的原始数据表的行和列都很大,所以我需要一个快速的解决方案。
非常感谢你。
数据表脚本编写回复:如何将两个表相乘您可以使用虚拟联接和公式列。
虚拟联接添加了蓝色和金色键
您可以将它们交互式地链接或与JSL链接。
这可能与JMP16名称一起使用, :“ parameter3 [Type]” n句法;您可能需要名称( “ parameter3 [Type]” ) 反而。
// make example tables. **save them** so virtual join works.
factorDT = New Table( "Imported Data 2",
New Column( "Type", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 2] ) ),
New Column( "parameter1", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1.1, 1.2] ) ),
New Column( "parameter2", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0.8, 0.9] ) ),
New Column( "parameter3", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1.1, 0.9] ) )
);
factorDT << save( "$temp/factor.jmp" );
dataDT = New Table( "Imported Data",
New Column( "ID", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 2, 3, 4] ) ),
New Column( "Type", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 1, 2, 2] ) ),
New Column( "parameter1", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 2, 3, 4] ) ),
New Column( "parameter2", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [4, 3, 2, 1] ) ),
New Column( "parameter3", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [2, 3, 3, 2] ) )
);
dataDT << save( "$temp/data.jmp" );
// join the tables
factorDT:type << Set Property( "Link ID", 1 );
dataDT:type << Set Property( "Link Reference", Reference Table( "factor.jmp" ) );
// add formula columns
dataDT << New Column( "p 1", Numeric, "Continuous", Format( "Best", 12 ), Formula( :"parameter1[Type]"n * :parameter1 ) );
dataDT << New Column( "p 2", Numeric, "Continuous", Format( "Best", 12 ), Formula( :"parameter2[Type]"n * :parameter2 ) );
dataDT << New Column( "p 3", Numeric, "Continuous", Format( "Best", 12 ), Formula( :"parameter3[Type]"n * :parameter3 ) );
// force the evaluation to complete
dataDT<<RUNFORMULAS></RUNFORMULAS>
回复:如何将两个表相乘而且,如果您不需要公式,则可以考虑矩阵的逐元素乘法,这可能会或可能不会“更好”。 在我的MacBook上,下面的代码花费了大约15秒钟。
NamesDefaultToHere(1);
nr = 10000; // Number of rows
nc = 10000; // Number of Columns
dt1 = AsTable(J(nr, nc, RandomInteger(1, 20)));
dt2 = AsTable(J(nr, nc, RandomUniform(0, 1)));
m1 = dt1 << getAsMatrix;
m2 = dt2 << getAsMatrix;
dtFinal = AsTable(m1 :*</img> m2);
回复:如何将两个表相乘感谢你的回复。 但是,我有很多列,列名很复杂。 因此,我想编写一个for循环来创建新列。 您可以对此声明进行更多解释吗?
Formula( :"parameter1[Type]"n * :parameter1 )
我正在考虑编写这样的for循环:
</CODE></PRE><PRE class="lia-code-sample language-markup"><CODE>colnames = dataDT << Get Column Names();
nc = ncols(dataDT);
DT = New Table("normalized", New Column("ID", values(dataDT:ID << get values)),
New Column("Type", values(dataDT:Type << get values)));
For(i = 3, i<= nc, i++,
DT << New Column( char( colnames[3]), Numeric, "Continuous", Format( "Best", 12 ), Formula( XXXX ) ));但我不知道如何正确构造公式。 它不断给我错误。 你能帮助我吗?
谢谢你。
回复:如何将两个表相乘谢谢,它适用于小型数据集。 但是我在表1中有几百万行,而在表2中只有两行。 因此,我想避免创建另一个巨大的表。 如果找不到更好的解决方案,请尝试这种方法。
回复:如何将两个表相乘循环并不容易;我认为这可以做到:
// make example tables. **save them** so virtual join works.
factorDT = New Table( "Imported Data 2",
New Column( "Type", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 2] ) ),
New Column( "parameter1", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1.1, 1.2] ) ),
New Column( "parameter2", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0.8, 0.9] ) ),
New Column( "parameter3", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1.1, 0.9] ) )
);
factorDT << save( "$temp/factor.jmp" );
dataDT = New Table( "Imported Data",
New Column( "ID", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 2, 3, 4] ) ),
New Column( "Type", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 1, 2, 2] ) ),
New Column( "parameter1", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 2, 3, 4] ) ),
New Column( "parameter2", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [4, 3, 2, 1] ) ),
New Column( "parameter3", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [2, 3, 3, 2] ) )
);
dataDT << save( "$temp/data.jmp" );
originalColumns = dataDT<<GETCOLUMNNAMES></GETCOLUMNNAMES>
- 在通过虚拟联接添加隐藏的列之前捕获原始列
- 使用\!“构建表达式e以转义嵌入的引号
- 使用write()调试您的工作,而不是打印
- << RunFormulas对于速度很重要;列公式的默认背景评估要慢得多