我有一個模板資料框,其中包含標記為 1-209 的 209 列。 通常,客戶會向我轉發包含組合列的資料集,例如,在新資料集中,可能會新增第 2 列和第 3 列(標記為「2 + 3」)。
如何提取新資料集中的列並將其用作公式以使模板資料框中的列相同? 我知道 JMP 有一個組合函數,但我如何編寫一個腳本來提取所有列名稱並將其轉換為公式以對齊兩個資料集。
我已在這封電子郵件中附加了範本和目標資料集的外觀。
你好@MikeDereviankin ,
我不確定我是否完全理解你的目的,但我想是的,並為此工作了大約一天。 (作為一名程式設計師,我的速度或 JSL 悟性不如@tx尼爾森,他知道他的東西!)。 我也沒有嘗試他的程式碼,但我以不同的方式思考它(我認為)。
下面的程式碼將從「目標」資料表中取得其中包含「+」的列名稱,在「範本」資料表中建立新的公式列,將這些特定列新增在一起。 然後,它將它們移動到資料表中的正確位置,並刪除新增在一起的原始列。 它還從列中刪除公式屬性。
花了大約一天的時間才找到呼叫列名稱的正確方法,因為它們是數字。 擁有角色名稱以便在沒有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
這是一個擴充組合列的簡單腳本。注意:模板資料表中的列名稱有錯誤。列名稱 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 )));