每行最後30條,刪除其他數據大家好,
我有一個包含多列的資料表,每列都有不同數量的行,其中包含資料。 我正在嘗試找到一種方法來獲取每列中最後 30 行的資料。 例如,第 1 列有 50 行,第 2 列有 75 行,我希望每行都有最後 30 行資訊。 有一個腳本可以做到這一點嗎?
自動化和腳本編寫資料混合與清理大規模客製化回覆:每行最後30條並刪除其他數據對於本曲目的未來讀者,更正@馬克貝利與 @txnelson 回應中的第二段程式碼相比,有關他的程式碼的聲明需要澄清。如果假設一旦找到缺失值,列中不會出現其他非缺失值,則這兩段程式碼將產生相同的結果。@馬克貝利代碼要求必須存在最小數量的非缺失值,其中 @txnelson 代碼不存在
回覆:每行最後30條並刪除其他數據FWIW,這是另一個使用矩陣的版本:
NamesDefaultToHere(1);
// Make a data table 'dt', to play with.
nr = 100; // Number of rows
nc = 30; // Number of columns
p = 0.7; // Proportion of missing values
vals = J(nr*nc, 1, RandomInteger(0, 99));
makeMissing = RandomIndex(nr*nc, Floor(nr*nc*p));
vals[makeMissing] = .;
dt = AsTable(Shape(vals, nr, nc));
dt << setName("Random Integers with some missing");
// Start here with the current table . . .
lastN = 30; // Number of non-missing values to retain
dt = CurrentDataTable();
vals = dt << getAsMatrix;
// Loop over columns . . .
newVals = [];
for(c=1, c<=NCol(vals), c++,
NMV = vals[LocNonMissing(vals[0, c]), c]; // Non-mising values in this column
if(NRow(NMV) >= lastN, // Do we have enough non-missing values?
lastNMV = NMV[(NRow(NMV) - lastN + 1)::NRow(NMV)]; // If so, get the 'lastN' of these . . .
newVals = newVals||lastNMV; // . . . and add them into a new column
);
);
dt2 = AsTable(newVals);
dt2 << setName("Last "||Char(lastN)||" non-missing Random Integers");回覆:每行最後30條並刪除其他數據試試這個版本。
Names Default to Here( 1 );
// create the example
dt = New Table( "Odd Lengths",
New Column( "Column 1", Numeric, Continuous, Values( 1::50 ) ),
New Column( "Column 2", Numeric, Continuous, Values( 1::75 ) ),
New Column( "Column 3", Numeric, Continuous, Values( 1::28 ) )
);
Wait( 0 );
// process the data table
For( col = N Col( dt ), col > 0, col--,
values = Column( dt, col ) << Get Values;
not missing = Sum( Not( Is Missing( values ) ) );
If( not missing < 30,
dt << Delete Columns( Column( dt, col ) ),
end = Contains( values, . ) - 1;
If( end == -1, end = N Row( dt ) );
view = end - 29 :: end;
Column( dt, col ) << Set Values( values[view] );
);
);
dt << Select Where( Row() > 30 ) << Delete Rows;
回覆:每行最後30條並刪除其他數據@馬克貝利這太棒了! 我開始意識到我的列中的資料小於 30 和大於 30 有沒有簡單的方法來只顯示這一點? 一個例子column 1 has 50 rows and column 2 has 75 rows and column 3 has 28 rows and I want each row to have the last 30 rows of info nothing that is below 30 so if it has 29 or less data points I would like to delete it也. 謝謝您的幫忙!
回覆:每行最後30條並刪除其他數據這是另一個版本,可以實現與第二個解決方案相同的結果@tx尼爾森
For( col = 1, col <= N Col( dt ), col++,
values = Column( dt, col ) << Get Values;
end = Contains( values, . ) - 1;
If( end == -1, end = N Row( dt ) );
view = end - 29 :: end;
Column( dt, col ) << Set Values( values[view] );
);
dt << Select Where( Row() > 30 ) << Delete Rows;
回覆:每行最後30條並刪除其他數據刪除資料表中除最後 30 行之外的所有行很容易。
Names Default To Here( 1 );
dt = Current Data Table();
dt << select where( Row() > N Rows( dt ) - 30 );
dt << delete rows;
如果您要求找到每列的最後 30 個非缺失數據點,然後將其保存為 n 列 x 30 行數據,那就有點困難了,我會質疑這樣做的有效性。這樣做的結果將消除不同列之間的資料點之間的幾乎所有關係。
Names Default To Here( 1 );
dt = Current Data Table();
// Copy the data table retaining no rows;
dtCopy = dt << subset( selected rows( 0 ), selected columns( 0 ) );
dtCopy << select where( Row() >= 0 );
dtCopy << delete rows;
For( i = 1, i <= N Cols( dtCopy ), i++,
theList = {};
For( k = N Rows( dt ), k >= 1, k--,
If( Is Missing( Column( dt, i )[k] ) == 0,
Insert Into( theList, Column( dt, i )[k] );
If( N Items( theList ) >= 30,
Break()
);
)
);
Column( dtCopy, i ) << set values( Reverse( theList ) );
);