大家好,
需要帮助开发一个脚本来对多列进行减法,列名和计数是可变的,
可以根据分析进行更改,
需要用同一行中所有值的差异填充列增量,
样本数据如下

预期输出:

如果你需要多次使用它,你也可以将它变成一个函数。
这是一个例子:
- 功能
- 使用正则表达式过滤到感兴趣的列
- 由于 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
你能举一个你想使用的公式的例子吗?这听起来像是我会在中使用自定义转换脚本工具插件为.
