我有一个存储临时值的 csv 文件。 我必须根据以下步骤找到某些样本集的组平均值:
1. 使所有小于 25 的临时值等于 0 iecolumn post_process
2. group_avg 列是通过忽略具有大于或等于 4 个连续零的行而得出的。 接下来计算该组样本的平均值,如下所示
3. Final_average列是group_avg列生成的结果
我们如何使用 jsl 编写此过程的脚本?
附加 csv 文件
最终结果为最终结果_average如下表所示:

代码的第二个版本更加高效。 考虑到您正在运行的数据量,我认为这会有所帮助。它应该提供相同的结果。如果没有,您可以尝试从代码的第一个版本中删除所有 Show() 语句,看看是否有效。
第一个脚本给出了正确的答案,但是两个脚本的输出不同,为什么你注释了代码的某些部分
我怀疑问题在于我错误地在代码中留下了几个“Show()”语句,这迫使大量写入 JMP 日志。
下面是“后处理”列规范,我认为该部分存在导致 JMP 挂起的问题。尝试运行它,看看它是否会运行而不会挂起
dt << New Column( "group_avg",
formula(
value = .;
If( Row() == 1,
flag = 0;
zeroCounter = 0;
);
If( flag == 0 & :post process > 0,
flag = 1;
captureMatrix = [];
);
If( :post process == 0 & flag == 1,
zeroCounter
++);
If( flag == 1,
captureMatrix = captureMatrix || Matrix( :post process );
If( zeroCounter == 4 | Row() == N Rows( dt ),
/*For( i = N Cols( captureMatrix ), i >= 1, i--,
If( captureMatrix[i] != 0,
Break(),
captureMatrix[i] = .
)
);*/
if( Row() == N Rows( dt ), zerocounter++ );
value = Mean( captureMatrix[ 1:: ncols(captureMatrix) - (zeroCounter - 1)] );
flag = 0;
zeroCounter = 0;
captureMatrix = [];
);
);
value;
)
);该脚本适用于小文件。 但如果是大文件,其中有 16000 个零而不是 4 个 JMP 就会停止响应。 有什么办法可以防止这种情况并运行脚本而不会挂起吗?
以下脚本仅使用 TEMP 列创建数据表,然后创建其余列并使用公式填充它们。
Names Default To Here( 1 );
dt = New Table( "temp_temp",
Add Rows( 54 ),
New Column( "TEMP",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values(
[23.23019577, 23.44946668, 23.56111955, 23.47601143, 23.36428639,
23.24016499, 32.42091411, 23, 32.26821731, 32.14990084, 32.42091411,
32.26821731, 32.14990084, 23.23019577, 23.44946668, 23.56111955,
23.47601143, 23.36428639, 23.24016499, 32.15353181, 32.16785998, 21,
32.502366, 32.16261133, 32.15757688, 32.16790474, 23.23019577,
23.44946668, 23.56111955, 23.47601143, 23.36428639, 23.24016499,
32.42091411, 10, 32.26821731, 32.14990084, 32.41334166, 32.17456755,
32.52383338, 32.17310331, 19.28571429, 19.28571429, 19.28571429,
19.28571429, 34.62948075, 34.28766971, 34.29470736, 34.61532486,
34.386807, 34.36554216, 34.58410721, 34.28726312, 23.57142857,
23.57142857]
)
)
);
dt << New Column( "post process", formula( If( :TEMP <= 25, 0, :TEMP ) ) );
dt:post process << delete formula;
dt << New Column( "group_avg",
formula(
value = .;
If( Row() == 1,
flag = 0;
zeroCounter = 0;
);
If( flag == 0 & :post process > 0,
flag = 1;
captureMatrix = [];
Show( "start", Row(), flag );
);
If( :post process == 0 & flag == 1,
zeroCounter
++);
If( flag == 1,
captureMatrix = captureMatrix || Matrix( :post process );
Show( "cap", Row(), captureMatrix );
If( zeroCounter == 4 | Row() == N Rows( dt ),
For( i = N Cols( captureMatrix ), i >= 1, i--,
If( captureMatrix[i] != 0,
Break(),
captureMatrix[i] = .
)
);
value = Mean( captureMatrix );
Show( value );
Show( Row(), captureMatrix, zeroCounter, Index( 1, N Cols( captureMatrix ) - (zeroCounter - 1) ) );
flag = 0;
zeroCounter = 0;
Show( flag );
captureMatrix = [];
);
);
value;
)
);
dt << new column("final Avg",
formula(
if(row()==1,
theRows = current data table() << get rows where(isMissing(:group_avg)==0);
);
If(row()<= nrows(theRows),
value=:group_avg[theRows[Row()]],
value=.
)
)
);