大家好,
需要協助開發一個腳本來對多列進行減法,列名和計數是可變的,
可以根據分析進行更改,
需要用同一行中所有值的差異填入列增量,
樣本資料如下

預期輸出:

如果你需要多次使用它,你也可以將它變成函數。
這是一個例子:
- 功能
- 使用正規表示式過濾到感興趣的列
- 由於 Filter Each 需要 JMP16
- 使用 Loc() 和矩陣計算
- 使用值建立新的列增量
Names Default To Here(1);
//example datatable
dt = New Table("Untitled",
Add Rows(5),
Compress File When Saved(1),
New Column("Column 1",
Numeric,
"Continuous",
Format("Best", 12),
Set Values([1, 0, 0, 1, 0]),
Set Display Width(55)
),
New Column("Column 2", Numeric, "Continuous", Format("Best", 12), Set Values([0, 2, 3, 0, 0])),
New Column("Column 3",
Numeric,
"Continuous",
Format("Best", 12),
Set Values([0, 3, 0, 0, 0]),
Set Display Width(87)
),
New Column("Column 4", Numeric, "Continuous", Format("Best", 12), Set Values([3, 0, 4, 0, 0])),
New Column("col", Character, "Nominal", Set Values({"a", "a", "a", "a", "a"}))
);
difference_between_min_max = Function({dt_ref, col_prefix_regex},
{Default Local},
//get columnn names of interest based on col_prefix
col_names_all = dt_ref << Get Column Names("Numeric", "String");
col_names_of_interest = Filter Each({val}, col_names_all,
!IsMissing(Regex(val, col_prefix_regex));
);
show(col_names_of_interest);
//all values for columns of interest, transpose for calculations
m = Transpose(dt_ref[0, col_names_of_interest]);
//replace 0 with missing to avoid calculation with those
m[Loc(m, 0)] = .;
//Get max and min values and deduct them
result = V Max(m) - V Min(m);
//Replace missing values with 0 for final results
result[Loc(result,.)] = 0;
//we could return result matrix here OR we can create new column with the function
dt_ref << New Column("delta", Numeric, Continuos, Set Values(result));
);
//example table in this example
difference_between_min_max(dt, "(Column )");
//example table by txnelson
//difference_between_min_max(Current Data Table(), "^(\D{1})$");
還可以把它變成自訂功能
假設您不知道名稱,也不知道列數,下面的程式碼將建立所需的公式,然後使用生成的公式建立新列
Names Default To Here( 1 );
dt = Current Data Table();
// Get the columns
colNames = dt << get column names( string, continuous );
// Build the formula
theExpr = "dt<<NEW column=""></NEW>

附上我使用的樣本資料表
這是我將使用的公式。它依賴矩陣函數 Loc(),該函數傳回矩陣中的所有非零條目。
x = :data[Row()] || :data1[Row()] ||
:data2[Row()] || :data3[Row()] ||
:data4[Row()] || :data5[Row()] ||
:data6[Row()] || :data7[Row()] ||
:data8[Row()] || :data9[Row()];
y = Loc( x );
Try( x[y[2]] - x[y[1]], . );
你好 ,
我正在編寫一份用於資料分析的腳本,並且需要在僅針對一個資料表的程式碼之間實作此邏輯,
你能幫忙指出我可以使用的程式碼嗎
先致謝
現在我明白了,在這種情況下,我個人會在腳本工具加入。我將選擇要轉換的所有列(我認為是 data1-data10),然後從“列”選單中選擇“自訂轉換”(安裝插件後可用)並輸入以下設定:

你好,謝謝你的回复,
我正在使用 JMP15 你能幫我指出我可以使用的程式碼嗎?
我是 JMP 的初學者
先致謝
你好@jinsejoseph
你能舉出一個你想使用的公式的例子嗎?這聽起來像是我會在中使用自訂轉換腳本工具插件為.
