如何獲得一組列的所有不同組合的總和?你好,
我正在使用 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>
我在那裡留下了一些其他選項,例如特定列的列表,或使用選定的列。
我希望這有幫助!
@朱利安