JSL:如何循环遍历列、生成图形生成器报告并将每个报告保存为 JPG 文件我正在尝试创建一个脚本,该脚本可以循环遍历每列的每个单元格,以计算小于或大于指定值的单元格数量。
如果我有一个包含 A ~ D 列和 1 ~ 10 行的表格,我想按行和列计算单元格的值。 换句话说,对于A列的所有行,有多少行符合我的标准。 同样,对于第 1 行的所有列,有多少列符合我的标准。 然后我会对所有行和列执行此操作。
这是我尝试开始的,但它什么也没返回。 对此有什么想法或更有效的方法吗?
Names Default To Here( 1 );
dt = Current Data Table();
Clear Log();
// Get all of the numeric columns in the data table
Numeric Columns = dt << get column names( numeric, string );
NRows = N Rows( dt );
Count = {};
//Column loop
For( i = 1, N Items( Numeric Columns ) <= 1, i++,
For Each Row(
dt,
If( Column(dt, Numeric Columns[i]) < 101,
Count = Count++;
Show( Count );
)
)
);
回复:JSL:如何循环遍历列、生成图形生成器报告并将每个报告保存为 JPG 文件您还可以用一行替换“对于每行”部分来计算每列的行数。这将利用数据表函数,而不需要查看每列的每一行。
COUNT_VAL=NROWS((DT<<SELECT where=""></SELECT>
回复:JSL:如何循环遍历列、生成图形生成器报告并将每个报告保存为 JPG 文件该脚本存在一些很容易修复的问题。
1. for循环的测试部分是错误的:
<SPAN class="token function">N Items</SPAN><SPAN class="token punctuation">(</SPAN> Numeric Columns <SPAN class="token punctuation">)</SPAN> <SPAN class="token operator"><=</SPAN> <SPAN class="token number">1</SPAN>
数字列的数量不会改变。 如果有 0 或 1 个数字列,则循环将永远运行,因为 0<=1 且 1<=1。 如果至少有 2 个数字列,则循环根本不会运行,因为 2(或更多)<=1 始终为 false。 测试应该是这样的:
i <= N Items( Numeric Columns )
这会测试您的迭代器 (i) 是否大于列数,并在大于时停止。
2. 在 For Each Row 循环中,您仍然需要对行进行下标,只是不必自己管理迭代。 所以这一段:
<SPAN class="token function">Column</SPAN><SPAN class="token punctuation">(</SPAN>dt<SPAN class="token punctuation">,</SPAN> Numeric Columns<SPAN class="token punctuation">[</SPAN>i<SPAN class="token punctuation">]</SPAN><SPAN class="token punctuation">)</SPAN> <SPAN class="token operator"><</SPAN> <SPAN class="token number">101</SPAN>
应该看起来像这样:
<SPAN class="token function">Column</SPAN><SPAN class="token punctuation">(</SPAN>dt<SPAN class="token punctuation">,</SPAN> Numeric Columns<SPAN class="token punctuation">[</SPAN>i<SPAN class="token punctuation">]</SPAN><SPAN class="token punctuation">)[Row()]</SPAN> <SPAN class="token operator"><</SPAN> <SPAN class="token number">101</SPAN>
3. 您的 Count 变量是一个列表,但您将其视为数字。 Count++ 不会对未填充数字的列表执行任何操作。 设置 Count = 0 而不是 Count = {}。 此外,Count++ 返回当前值,然后将 Count 设置为 Count +1。 所以 Count = Count++ 永远不会改变 Count 的值。 (示例:计数 = 0。 计数 = 计数++。 计数仍然等于零。)
这是经过上述更改的完整脚本。 最终计数是所有数字列中小于 101 的单元格数。
Names Default To Here( 1 );
dt = Current Data Table();
Clear Log();
// Get all of the numeric columns in the data table
Numeric Columns = dt << get column names( numeric, string );
NRows = N Rows( dt );
Count = 0;
//Column loop
For( i = 1, i <= N Items( Numeric Columns ), i++,
For Each Row(
dt,
If( Column(dt, Numeric Columns[i])[Row()] < 101,
Count++;
Show( Count );
)
)
);
哈特哈,
梅兰妮
回复:JSL:如何循环遍历列、生成图形生成器报告并将每个报告保存为 JPG 文件@阿卜杜尔吉,
您可以使用矩阵来识别要测试的条件。 这是一个例子,
Clear Log();
// Open a sample data set
dt = Open( "$SAMPLE_DATA/Cars.jmp" );
// Extracting numeric column names
ColNames = dt << Get Column Names("String");
NumericCols = [];
for(nCol = 1, nCol <= N Items(ColNames), nCol++,
DType = Column(dt,nCol) << Get Data Type;
If(DType == "Numeric",
NumericCols = NumericCols || nCol;
);
);
NumericColNames = ColNames[NumericCols];
// Extract numeric data only
Mat = dt << Get As Matrix;
// Row Wise Test
// For e.g. Find all rows where year < 90
YearCondition = Loc(Mat[0,1] < 90);
Show(Mat[YearCondition,0]);