刪除重複的列你好:
我有一個 hugue 數據表,其中包含 400 多列和 50,000 行。 我有 30-40 個重複的列,我想將它們從我的數據表中刪除。有任何想法嗎?
回复:刪除重複的列這是一個蠻力(和未經測試的)替代方案,對於預期用途可能會或可能不會“更好”:
NamesDefaultToHere(1);
dt = CurrentDataTable();
// Make a matrix 'mat' holding pairwise equality of column values
n = NCol(dt);
mat = J(n, n, .);
for(c1 = 1, c1 <=n, c1++,
for(c2 = c1+1, c2 <= n, c2++,
c1Vals = Column(dt, c1) << getValues;
c2Vals = Column(dt, c2) << getValues;
mat[c1, c2] = all(c1Vals == c2Vals);
);
);
// Convert 'mat' to a table
cols = dt << getColumnNames("String");
dtn = dt << getName;
dt2 = AsTable(mat, << columnNames(cols));
dt2 << setName("Duplicate Columns in "||dtn);
dt2 << NewColumn("Column", Character, Values(cols));
dt2 << moveSelectedColumns({"Column"}, ToFirst);回复:刪除重複的列你可以使用MD5校驗和, 由斑點 MD5()函數生成列中值的散列,然後比較列之間的校驗和。
您可以使用關聯數組來檢查生成的重複 md5 校驗和,以避免必須自己將每個校驗和與其他校驗和進行比較。
dt = New Table( "Some Duplicate Columns",
Add Rows( 1000 ),
New Column( "Column 1", Numeric, "Continuous", Format( "Best", 12 ), Formula( Random Normal() ) ),
New Column( "Copy of Column 1", Numeric, "Continuous", Format( "Best", 12 ), Formula( :Column 1 ) ),
New Column( "Not a copy of Column 1",
Numeric,
"Continuous",
Format( "Best", 12 ),
Formula( If( Row() == 1, 1, :Column 1 ) )
),
New Column( "Character copy of Column 1", Character, "Nominal", Formula( Char( :Column 1 ) ) ),
New Column( "Character copy of Copy of Column 1",
Character,
"Nominal",
Formula( Char( :Copy of Column 1 ) ),
)
);
//get values into matrix (numeric columns) or list (character columns)
c1 = :column 1 << get values;
c2 = :Copy of Column 1 << get values;
//avoid having to check column data types when converting the values to blob
//by converting matrix or list to character
//
//if you're not lazy you could use the Matrix to Blob() function for numeric columns
//and the Char to Blob() function for character columns
char_c1 = Char( c1 );
char_c2 = Char( c2 );
c1_md5 = Blob MD5( Char To Blob( char_c1 ) );
c2_md5 = Blob MD5( Char To Blob( char_c2 ) );
Show( c1_md5 == c2_md5 );
//check the whole data table using hex version of md5 as keys of associative array
aa = Associative Array();
For( i = 1, i <= N Col( dt ), i++,
md5 = Hex( Blob MD5( Char To Blob( Char( Column( dt, i ) << get values ) ) ) );
//check to see if the associate array already has a key named with this md5
If( aa << Contains( md5 ),
//aa does contain md5
Print( aa[md5] || " is a duplicate of " || (Column( dt, i ) << get name) ),
//aa does not contain md5
//store a key named with this md5 with a value of the column name
aa[md5] = Column( dt, i ) << get name;
);
);
回复:刪除重複的列為什麼你有這麼多重複的列? 這個結果可以避免嗎?
回复:刪除重複的列你好,
我今天遇到了同樣的問題,這是我刪除重複列的腳本。
如果您有一個包含數千行的表,那真的很花時間。 應該有一個更有效的方法來做到這一點,希望有人能弄清楚並分享。
Names Default To Here( 1 );
col_name = dt << get column names;
dup_col = {}; //store the duplicated column name
same_c = 0; //to record how many rows are the same while doing columns comparison
/* When the elements are the same in the same row for 2 columns, same_c will be added by 1*/
/*If the same_c is equal to the total row number, then the 2 columns are the same. Insert the column name into dup_col*/
For( i = 1, i <= (N Cols( dt ) - 1), i++,
For( k = i + 1, k <= N Cols( dt ), k++,
(For Each Row( If( :(col_name[i])[Row()] == :(col_name[k])[Row()], same_c++, Wait( 0 ) ) ) ;
If( same_c == N Rows( dt ), Insert Into( dup_col, col_name[k] ), Wait( 0 )) ;
same_c = 0 ; )
)
);
dt << Delete Column( Eval( dup_col ) );
// delete the duplicated columns
回复:刪除重複的列我不知道有任何內置的 JMP 方法可以執行此操作。 但是,您可以使用 R 方統計量來確定列是否相同。
如果您要以交互方式執行此操作,則可以使用以下方法識別連續列
分析==>多元方法==>多元
它會給你一個相關矩陣,你可以將它輸出到數據表中,然後用它來識別哪些列的相關係數為 1。
可以通過在 Fit Y by X 中的 RSquare 表中創建“製作組合數據表”來識別序數/名義列。 再一次,RSquare 為 1.0 將提示您列是相同的。
現在,如果您要編寫解決方案腳本,則可以很容易地將上述內容自動化。
也許另一個社區成員會有更好的解決方案