如何获得一组列的所有不同组合的总和?你好,
我正在使用 JMP PRO v16
我有一组包含数值的 10 列,我想尝试获得这 10 列之间所有可能组合的分数总和(例如,A 列 + B 列、A 列 + B 列 + C 列、B 列 + C 列等)。
非常感谢你的帮助
自动化和脚本编写回复:如何获得一组列的所有不同组合的总和?非常感谢吉姆的帮助!
回复:如何获得一组列的所有不同组合的总和?这是我之前提交的代码的修改。此代码为前 10 列的每个组合添加一个新列。它添加了 1013 个新列。请检查数据表添加的正确性。
Names Default To Here( 1 );
dt = data table("sum combined");
startCol = 2;
endCol = N Col( dt ) ;
colNames = dt << get column names( string );
// Build Combinations
For( nInCombo = 2, nInCombo <= endCol, nInCombo++,
combos = NChooseK Matrix( endCol, nInCombo );
For( row = 1, row <= N Rows( combos ), row++,
// create the column name and the formula for the new column
name = "";
cols = N Cols( combos );
code = "Sum( :\!"" || colNames[combos[row, 1, 1]] || "\!"n";
name = "\!"" || colNames[combos[row, 1, 1]];
For( col = 2, col <= cols, col++,
code = code || ", :\!"" || colNames[combos[row, col, 1]] || "\!"n";
name = name || " + " || colNames[combos[row, col, 1]];
);
name = name || "\!"";
code = code || " )";
eval(parse("dt << new column(" || name || ", formula(" || code || "));"));
// Uncomment the next line to remove the formula for the column. This
// will probably make the data table run faster
//column(dt,ncols(dt)) << delete property("formula");
);
);回复:如何获得一组列的所有不同组合的总和?嗨吉姆,
非常感谢你的帮助,
这正是我想要的,如果可能的话,我需要获得一个包含每个组合总和的列(2、3、4、5、6、7、8 和 9 列的所有可能组合)
我刚刚开始 JSL,非常感谢您的帮助,
回复:如何获得一组列的所有不同组合的总和?我真的不清楚最终结果到底是什么。但是,下面的脚本汇总了具有 7 个(不是 10 个,但适用 10 个)数字列的数据表的所有列组合。它创建一个新表,其中包含所创建的每个列组合的总和。

Names Default To Here( 1 );
dt =
// Open Data Table: Blood Pressure.jmp
// → Data Table( "Blood Pressure" )
Open( "$SAMPLE_DATA/Blood Pressure.jmp" );
startCol = 3;
endCol = N Col( dt ) - startCol - 1;
colNames = dt << get column names( string );
Remove From( colNames, 1, startCol - 1 );
dtOutput = New Table( "output", New Column( "Columns Used", character ), New Column( "Sum" ) );
// Build Combinations
For( nInCombo = 2, nInCombo <= endCol, nInCombo++,
combos = NChooseK Matrix( endCol, nInCombo );
Show( endcol, combos );
For( row = 1, row <= N Rows( combos ), row++,
Current Data Table( dt );
name = "";
cols = N Cols( combos );
code = "x=Sum( col sum( :\!"" || colNames[combos[row, 1, 1]] || "\!"n)";
name = colNames[combos[row, 1, 1]];
For( col = 2, col <= cols, col++,
code = code || ", col sum( :\!"" || colNames[combos[row, col, 1]] || "\!"n)";
name = name || " + " || colNames[combos[row, col, 1]];
);
code = code || " )";
Eval( Parse( code ) );
dtOutput << add rows( 1 );
dtOutput:Columns Used[N Rows( dtOutput )] = name;
dtOutput:Sum[N Rows( dtOutput )] = x;
);
);
如果您希望逐行完成求和,可以轻松更改我的代码以在现有数据表中为每个组合创建一个新列
回复:如何获得一组列的所有不同组合的总和?太感谢了! 我认为这个脚本只产生成对组合,有没有办法拥有所有可能的组合? (即 2、3、4、5、6、7、8 和 9 列的所有可能组合),
再次感谢你的帮助,
回复:如何获得一组列的所有不同组合的总和?你好@DivisiveFerret8 .,
我不知道在 JMP 中以交互方式快速执行此操作的方法,但使用一些 jsl,我们可以快速创建这些列:
Names Default to Here (1);
dt = Current Data Table();
// Get the number of columns
ncols = N Col( dt );
// Nested loop to go through all pairwise combinations of columns
For( i = 1, i < ncols, i++,
For( j = i + 1, j <= ncols, j++,
colName1 = Column( dt, i ) << get name;
colName2 = Column( dt, j ) << get name;
// create new column
newColName = colName1 || " + " || colName2;
dt << New Column( newColName, Numeric, "Continuous", Format( "Best", 12 ) );
// compute the sum
Column( dt, newColName ) << set each value(
Column( dt, colName1 )[Row()] + Column( dt, colName2 )[Row()]
);
)
);
//编辑//
上面的代码适用于表中列的所有组合,但我现在看到您在询问一组列:
Names Default to Here (1);<BR />dt = Current Data Table();
// List of specific columns
// columns = {"Column A", "Column B", "Column C", "Column D", "Column E", "Column F", "Column G", "Column H", "Column I", "Column J"};
// Using selected columns
// columns = dt << Get Selected Columns;
// Referencing by Group Name
columns = dt << get column group( "XY" );
// Number of specific columns
ncols = N Items( columns );
// Nested loop to go through all pairwise combinations of specified columns
For( i = 1, i < ncols, i++,
For( j = i + 1, j <= ncols, j++,
colName1 = columns[i]<<GET name=""></GET>
我在那里留下了一些其他选项,例如特定列的列表,或使用选定的列。
我希望这有帮助!
@朱利安