我有一个模板数据框,其中包含209列标记为1-209的列。 客户经常会转发给我包含合并列的数据集,例如,可以在新数据集中添加第2列和第3列(标记为“ 2 + 3”)。
如何提取新数据集中的列并将其用作公式以使列与模板数据框中的列相同? 我知道JMP具有合并功能,但是如何编写一个脚本来提取所有列名并将其转换为公式以对齐两个数据集。
我已在此电子邮件中附加了模板和目标数据集的外观。
这是一个简单的脚本,在合并的列上展开。注意:“模板数据”表中的列名有误。列名95出现两次。第二个应该是98。
Names Default To Here( 1 );
// Point to the 2 data tables....assumption is they are open
dtTemplate = Data Table( "Template_Template" );
dtTarget = Data Table( "Target_Target" );
// Create a template data table with just the column names
dtHeader = dtTemplate << subset( selectd rows( 0 ), selected columns( 0 ) );
dtHeader << select where( Row() > 0 );
dtHeader << delete rows;
// Change the column data type to match in incoming target data
dtHeader:Column 1 << datatype( character );
// Concatenate the data together. JMP will match all columns with the
// same name, and create new columns for ones that don't have a match
dtInterim = dtHeader << concatenate( dtTarget );
// Loop across the new columns and assign the values back to the columns
// the values need to be in
For( i = N Cols( dtHeader ) + 1, i <= N Cols( dtInterim ), i++,
start = Num( Word( 1, Column( dtInterim, i ) << get name, "+" ) );
end = Num( Word( 2, Column( dtInterim, i ) << get name, "+" ) );
For( k = start, k <= end, k++,
Column( dtInterim, Char( k ) )[1] = Column( dtInterim, i )[1]
);
);
// Delete the extra columns
dtInterim << delete columns( index(N Cols( dtHeader ) + 1, N Cols( dtInterim )));
你好@MikeDereviankin ,
我不确定我是否完全了解您的需求,但我是这样认为的,并做了大约一天的工作。 (我不如程序员或精通JSL的人快@txnelson ,他知道自己的东西!)。 我也没有尝试过他的代码,但是我以不同的方式思考(我认为)。
下面的代码将从“目标”数据表中使用带有“ +”的列名,在“模板”数据表中创建一个新的公式列,将这些特定列加在一起。 然后,将它们移动到数据表中的适当位置,并删除添加在一起的原始列。 它还将从列中删除公式属性。
因为它们是数字,所以花了大约一天的时间寻找正确的方式来调用列名称。 不用Eval()和Parse()等命令来使用字符名称要容易得多。我敢肯定,有更好的人可以使它更雅致,但这是可行的(至少我解释了您的询问方式)有关如何执行此操作)。 我在其中放置了一个Wait()命令,但是您可能不需要它。
Names Default To Here( 1 );
//defines the two data tables, they must be open already and named appropriately
dt_target = Data Table( "Target" );
dt_template = Data Table( "Template" );
//This gets the column names for each table
target_col_names = dt_target << get columnnames( Numeric, string );
template_col_names = dt_template << get column Names( Numeric, string );
//This tests the column names in the target data table to see if there is a "+" and if so puts that column name in the target_col_keep list
target_col_keep = {};
For( i = 1, i <= N Cols( dt_target ) - 1, i++,
If( Contains( target_col_names[i], "+" ) != 0,
Insert Into( target_col_keep, target_col_names[i] )
)
);
//This loops through the items in target_col_keep to make new columns and add formulas.
//The complicated part of As Column(Num(Word(Eval(EvalExpr(Expr()))))) basically converts the number names to a reference-able column.
//The extra +1 is there because of the first column in the dt_template data table -- it offsets the column numbers on down the line in <BR />//reference to the target data table.
//The first word command gets only the first column of the added up to the "+", hence Word(1,...)
//The second word command gets only the second word after the "+", hence Word(2,...)
//The new columns are added to the end of the data table.
i=.;//reset counter
For( i = 1, i <= N Items( target_col_keep ), i++,
dt_template << New Column( target_col_keep[i],
"Numeric",
"continuous",
Formula(
As Column( (Num( Word( 1, Eval( Eval Expr( Expr( target_col_keep[i] ) ) ), "+" ) ) + 1) ) + As Column(
(Num( Word( 2, Eval( Eval Expr( Expr( target_col_keep[i] ) ) ), "+" ) ) + 1)
)
)
);
//this step moves the newly created column after the second column of the sum. Comment out if you don't want to move them.
prevcol = Num( Word( 1, Eval( Eval Expr( Expr( target_col_keep[i] ) ) ), "+" ) );
dt_template << select Columns( As Column( Eval( Eval Expr( Expr( target_col_keep[i] ) ) ) ) ) <<
Move Selected Columns( After( Column( Parse( ":Name(\!"" || Char( prevcol ) || "\!")" ) ) ) );
);
Wait(0.5);
//This part removes the formula. Comment out if you don't want to remove the formula.
j = .;//reset counter
For( j = 1, j <= N Col( dt_template ), j++,
As Column( j ) << Delete property( "Formula" )
);
//This deletes the old columns that were added together. Comment out if you don't want to delete them.
k=.;//reset counter
For( k = 1, k <= N Items( target_col_keep ), k++,
str1 = Num( Word( 1, Eval( Eval Expr( Expr( target_col_keep[k] ) ) ), "+" ) );
str2 = Num( Word( 2, Eval( Eval Expr( Expr( target_col_keep[k] ) ) ), "+" ) );
dt_template << Delete Columns( Column( Parse( ":Name(\!"" || Char( str1 ) || "\!")" ) ) );
dt_template << Delete Columns( Column( Parse( ":Name(\!"" || Char( str2 ) || "\!")" ) ) );
);希望这可以帮助!,
DS