循环?计算多个列以及在分析之间更改名称的列名称(日期)的缺失数据我有一个问题,希望这个社区可以为我提供帮助。 我希望添加一列,以显示从最后一天开始倒退的连续天数。
我还希望添加一列来计算7列中结果高于0.02的天数。
我已经通过使用一系列if语句找到了两种解决方案,但是希望了解是否可以通过更优雅的方式(例如使用循环)来完成。
最后,表格中的日期每周都会更改,因此我希望此公式与列名称无关。 这可能吗?
我在解决方案中附加了一个看起来类似的数据表。
先感谢您
/马克库
脚本编写回复:循环吗?计算多个列以及在分析之间更改名称的列名称(日期)的缺失数据这是我想出的替代公式
If( Row() == 1,
start = 2;
end = 8;
);
count = end;
While( Is Missing( Column( count )[Row()] ) == 1 & count >= start, count-- );
end - count;
回复:循环吗?计算多个列以及在分析之间更改名称的列名称(日期)的缺失数据我的解决方案很可能比必要的要复杂一些(example_new表创建附带完整的脚本)。 此解决方案希望将感兴趣的列命名为“ Max(something)”(即使超过七个“ Max(”列)也应该可以使用。
这两个公式列都将失去它们根据数据更改进行重新计算的能力,并且如果数据表上的数据发生更改,则必须从表红色三角形“手动”重新运行它们:

列的公式:
//modified idea based on of @txnelson solution
tempCol = dt << New Column("# last days missing_community", Numeric, Nominal,
Formula(
Local({
//get information on column names
dt = Current Data Table(),
colNames = dt << Get Column Names(String)
},
//get list of column names with only Max( in name
For(i = N Items(colNames), i > 0, i--,
If(!Contains(colNames[i], "Max("),
Remove From(colNames, i);
)
);
//get values on rows
//this might make a mistake if columns before Max( have are missing values
matLength = N Items(colNames) + 1;
rowValues = dt[Row(), 0][1::matLength];
count = 0;
//calculate missing values from the end of matrix and stop on first found value
While(Is Missing(rowValues[matLength]), count++; matLength--);
count;
)
)
);
dt << New Column("#days higher than 0,02_community", Numeric, Nominal,
Formula(
Local({
//get information on column names
dt = Current Data Table(),
colNames = dt << Get Column Names(String)
},
//get values on row and set values of colums without Max( in name to 0
rowVals = dt[Row(), 0];
For(i = N Items(colNames), i > 0, i--,
If(!Contains(colNames[i], "Max("),
rowVals[i] = 0;
)
);
//create matrix with 0 and 1 based on if value is over 0.02
rowVals_over = rowVals > 0.02;
//get sum of previous matris to calculate Max( columns with values over 0.02
Sum(rowVals_over);
);
)
);
回复:循环吗?计算多个列以及在分析之间更改名称的列名称(日期)的缺失数据亲@马克库,
我还努力创建动态脚本并将其解析为列公式。 在过去,我遇到的情况与您类似,在这种情况下,重复测量的次数(在您的情况下,每天都会重复执行)可能从一个观察值(批次)更改为另一个观察值。
我的解决方案可能满足您的需求,但我认为应该可以。
请在使用前检查它是否对您可能遇到的不同情况具有鲁棒性。
在数据表打开的情况下,运行此脚本并检查结果。
Names Default To Here( 1 );
dt = Current Data Table();
// get list of columns
Cols = dt << get column names ( Continuous );
maxlist = {};
For( i = 1, i <= N Items( cols ), i++,
If( Contains( cols[i], "Max" ),
Insert Into( maxlist, cols[i] )
)
);
dt2 = dt << Stack(
columns(maxlist ),
Source Label Column( "Time" ),
Stacked Data Column( "Data" )
);
dt2 << Sort(
By( :Batch#, :Time ),
Order( Ascending, Descending ),
replace table
);
dt2 <<NEW column=""> :Index2 ) << delete rows ();
dt3 = dt2 << Summary(
Group( :Batch# ),
Freq( "None" ),
Weight( "None" ),
Link to original data table( 0 ),
);
close (dt2, no save);
dt3:N Rows << set name ("No of last days missing");
dt << new column ("No of last days missing", set each value (0));
dt << Update(
With( dt3 ),
Match Columns( :Batch# = :Batch# )
);
close (dt3, no save);
//
dt5 = dt << Stack(
columns(maxlist ),
Source Label Column( "Time" ),
Stacked Data Column( "Data" )
);
dt5 << select where (ismissing (:Data)) << delete rows;
dt5 << select where (:Data<=0.02) << delete rows;
dt6 = dt5 << Summary(
Group( :Batch# ),
Freq( "None" ),
Weight( "None" ),
Link to original data table( 0 )
);
close (dt5, no save);
dt6:N Rows << set name ("Days over 0.02");
dt << new column("Days over 0.02", set each value(0));
dt<< Update(
With( dt6 ),
Match Columns( :Batch# = :Batch# )
);
close (dt6, no save);
<BR /></NEW>
让我们知道它是否有效,
罗恩