删除重复的列你好:
我有一个 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 将提示您列是相同的。
现在,如果您要编写解决方案脚本,则可以很容易地将上述内容自动化。
也许另一个社区成员会有更好的解决方案