循環?計算多個列以及在分析之間更改名稱的列名稱(日期)的缺失數據我有一個問題,希望這個社區可以為我提供幫助。 我希望添加一列,以顯示從最後一天開始倒退的連續天數。
我還希望添加一列來計算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>
讓我們知道它是否有效,
羅恩