JSL - 查找缺失數據的百分比我有一個包含 3 列的數據表

我需要創建一個表,顯示 HEIGHT 列中按 ID 和 SUB_ID 分組的缺失數據的百分比。 在上面的情況下,結果應該是:

但在某些情況下,可能沒有任何行丟失數據。 在這種情況下,缺失百分比應為 0。

我需要幫助為此編寫 JSL 腳本。 我將不勝感激任何指導。 我的腳本中存在以下三個問題:
1. 如何跟踪數據表的句柄/引用,以便我可以隨時調用它。 我總是在腳本的開頭使用“dt = CurrentDataTable”。 但是在創建了一些匯總表/子集表之後,我失去了對原始表的引用,並且不知道如何使其成為當前表。
2. 即使沒有丟失行,我也需要它工作。 現在腳本只是停在中間。
3. 我需要關閉所有牌桌,除了缺席百分比的決賽桌之外。
// Read the original table
dt = CurrentDataTable();
main_table = Data Table( dt ) << get name; // get the table name
// Create a summary table with no of rows grouped by ID and SUB_ID
dt_summary = dt<< Summary(Group(:ID, :SUB_ID), Freq("None"), Weight("None"), output table name( "Main Table Summary"));
:N Rows << Set Name( "Total No of Rows" );
// Create a subset of the original table with only the missing rows
Current Data Table(Data Table(main_table));
dt = CurrentDataTable();
dt_missing = dt << select where( Is Missing( :HEIGHT ) == 1 ) << Subset(Output Table Name("Missing Rows"));
If( !Is Empty( dt_missing ),
dt_missing_summary = Data Table( dt_missing ) << Summary(
Group( :ID, :SUB_ID ),Freq("None"), Weight("None"), output table name( "Missing Rows Table Summary"));
:N Rows << Set Name( "No of Rows with Missing Data" );
dt_missing_summary << Update(
With( Data Table( dt_summary ) ),
Match Columns( :ID = ID, :SUB_ID = :SUB_ID ),
Add Columns from Update table( :Total No of Rows )
);
New Column( "PercentMissing",
Numeric,
"Continuous",
Format( "Percent", 12, 2 ),
Formula( :No of Rows with Missing Data / :Total No of Rows )
);
);
//Close( dt_missing, nosave );
//Close( dt_summary, nosave );謝謝
數據混合和清理回复:JSL - 查找缺失數據的百分比感謝您的答复。 直到現在我才知道 N Missing 可以用來查找丟失的數據行。 它使工作變得更加容易,並減少了腳本中的幾個步驟。回复:JSL - 查找缺失數據的百分比感謝您的逐步解釋。 步驟1、2和4是我學到的新技巧,非常有幫助。 如果沒有丟失數據,難怪我的腳本會失敗。 當應用多個條件時,我會記得拆分語句。回复:JSL - 查找缺失數據的百分比感謝您提供簡單而緊湊的解決方案。 它確實在原始表中添加了一個額外的列,但這對我來說不是問題。回复:JSL - 查找缺失數據的百分比如果您之前看到過此內容,請重新閱讀。 有一個錯誤。
使用 JMP 的內置函數 Col N Missing ( ) 可簡化此任務。 我將展示兩種方法。
解決方案#1:
- 在第一個表中創建一列以按組計算缺失值。
- 總結一下第一張表。
- 更改 N 行列的名稱(如果需要)並為比率創建一個公式列。
Names Default To Here( 1 );
//these 2 lines just create a table with overall missing rate of 35% for the weight varable.
dt = Open( "$Sample_Data\Car Physical Data.jmp" );
dt:weight[Loc( J( 116, 1, Random Binomial( 1, .35 ) ) == 0 )] = .;
//add a formula column to compute n missing rows.
dt << New Column( "Missing Rows", formula( Col N Missing( :weight, :country, :type ) ) );
//create summary table, rename N Rows column, and add a % missing formula column.
dtSum = dt << Summary( Group( :Country, :Type, :missing rows));
dtSum:N Rows << set name("Total Rows");
dtSum << new column ("Pct Missing", formula(:missing rows / :total rows));
解決方案#2:
- 在第一個表中創建所有公式列,然後進行直接匯總。
- 或者,從摘要表中刪除 N 行列。
** 此方法中的“陷阱”位於 Total Rows ( ) 函數中。 您需要以“1”作為第一個參數的開頭,以用作虛擬列,因為第一個參數不能用作分組列。 省略 1,您將看到計數僅反映“類型”組中的行數...而不是國家/地區和類型的組合。
乾杯,並對第一次出現的錯誤表示歉意。
Names Default To Here( 1 );
//don't worry about these; these are just to get a table present with some missing values @ an
//overall missing rate of 35%
dt = Open( "$Sample_Data\Car Physical Data.jmp" );
dt:weight[Loc( J( 116, 1, Random Binomial( 1, .35 ) ) == 0 )] = .;
//create formula columns using built-in functions to get what you're after.
dt << New Column( "Missing Rows", formula( Col N Missing( :weight, :country, :type ) ) );
dt << New Column( "Total Rows", formula( Col Number( 1, :country, :type ) ) );
dt << New Column( "Group Pct Missing", formula( :missing rows / :total rows ) );
//view summary table.
dtSum = dt << Summary( Group( :Country, :Type, :missing rows, :total rows, :group pct missing));
回复:JSL - 查找缺失數據的百分比- 你需要添加
Names Default To Here( 1 );
要限制腳本中指定的變量不被其他腳本中同名的變量更改,請更改當前腳本中的值
- 看來您對 Current Data Table() 函數有誤解。它可以通過兩種不同的方式使用,並且將幫助您不丟失對原始數據表的引用。規格
dt = Current Data Table();
你一直在正確使用。當稍後在腳本中,您想要將原始數據表設為“當前數據表”時,您所要做的就是current data table(dt);
這將告訴 JMP 使用“dt”作為當前數據表。您需要避免做的是將“dt”放置在“=”的左側來重新分配它。
- 有時,將操作組合在單個語句中並不是一件好事。你的
dt_missing = dt << select where( Is Missing( :HEIGHT ) == 1 ) << Subset(Output Table Name("Missing Rows"));期望在一個語句中選擇行和子集表。問題是,如果沒有任何缺失的 :Height 值,它將無法為您提供所需的內容。在我對腳本的修改(如下)中,dt 數據表是否選擇了行,然後檢查是否找到了丟失的行,如果是,則只有然後繼續處理dt << select where( Is Missing( :HEIGHT ) == 1 );
If( N Rows( dt << get selected rows ) > 0,
- Try() 函數在運行可能失敗的函數時非常方便,如果失敗,您不希望腳本停止運行。您正在使用的 Close() 函數非常適合使用 Try() 函數
Try( Close( dt_missing, nosave ) );
Try( Close( dt_summary, nosave ) );
我希望這對您有幫助......如果您有更多問題,請回复
Names Default To Here( 1 );
// Read the original table
dt = Current Data Table();
//main_table = Data Table( dt ) << get name; // get the table name
// Create a summary table with no of rows grouped by ID and SUB_ID
dt_summary = dt << Summary(
Group( :ID, :SUB_ID ),
Freq( "None" ),
Weight( "None" ),
output table name( "Main Table Summary" )
);
:N Rows << Set Name( "Total No of Rows" );
// Create a subset of the original table with only the missing rows
//Current Data Table(Data Table(main_table));
//dt = CurrentDataTable();
dt << select where( Is Missing( :HEIGHT ) == 1 );
If( N Rows( dt << get selected rows ) > 0,
dt_missing = dt << Subset( Output Table Name( "Missing Rows" ), selected rows( 1 ) );
dt_missing_summary = Data Table( dt_missing ) <<
Summary(
Group( :ID, :SUB_ID ),
Freq( "None" ),
Weight( "None" ),
output table name( "Missing Rows Table Summary" )
);
:N Rows << Set Name( "No of Rows with Missing Data" );
dt_missing_summary << Update(
With( Data Table( dt_summary ) ),
Match Columns( :ID = ID, :SUB_ID = :SUB_ID ),
Add Columns from Update table( :Total No of Rows )
);
New Column( "PercentMissing",
Numeric,
"Continuous",
Format( "Percent", 12, 2 ),
Formula( :No of Rows with Missing Data / :Total No of Rows )
);
);
Try( Close( dt_missing, nosave ) );
Try( Close( dt_summary, nosave ) );
回复:JSL - 查找缺失數據的百分比@jthi ,
我們同時沿著同樣的思路思考,我喜歡你的答案比我的更完整,並且同意第 1 條和第 3 條建議。
回复:JSL - 查找缺失數據的百分比首先,我強烈建議查看 JMP 腳本指南: JMP15 腳本編寫指南和腳本索引可從 Jmp/Help/Scripting Index 中找到。 這些是 JSL 腳本編寫的非常好的資源。
要完成此任務,您不需要任何 jsl:
1. 讓我們從您提供給我們的示例表開始,其中有兩個缺失值
2. 創建表的摘要,其中 N Missing with Height 作為統計數據,SUB_ID 和 ID 作為 Group


3. 通過選擇 N 行和 N 缺失(高度)列來創建新公式

4. 將“列屬性”中的格式更改為“百分比”,並根據需要重命名列
基於這些步驟,您還可以相當輕鬆地使用 JMP 提供的功能來顯示將此手動流程更改為自動流程的步驟所需的腳本(應進行一些小的更改以使其更加穩健)。
對於你的問題1和3:
1.這在很大程度上取決於您如何創建/打開/獲取數據表。 如果您始終具有相同的名稱,則可以引用該名稱,但從長遠來看,我不會相信這一點,因為創建具有相同名稱的多個數據表非常容易,並且 JMP 會在名稱後添加索引。 但這些可以通過Datatable(nameofdatatable)來引用。
3. 您可以使用 Get Data Table List() 獲取數據表列表。 循環數據表並關閉不必要的數據表。 但在我看來,您應該始終通過“關閉”(引用數據表,不保存)盡快關閉不需要的數據表。
執行該任務的示例腳本:
Names Default To Here(1);
//create example data and add reference to dt variable
dt = New Table("exampledata",
Add Rows(8),
Compress File When Saved(1),
New Column("ID",
Numeric,
"Nominal",
Format("Best", 12),
Set Values([1, 1, 1, 1, 2, 2, 2, 2])
),
New Column("SUB_ID",
Character,
"Nominal",
Set Values({"A", "A", "B", "B", "A", "A", "B", "B"})
),
New Column("HEIGHT",
Numeric,
"Continuous",
Format("Best", 12),
Set Values([10, 11, ., 9, ., 12, 8, 12])
)
);
//note Link to original data table(0)
//dt_summary is reference to the new Summary table
dt_summary = dt << Summary(
Group(:ID, :SUB_ID),
N Missing(:HEIGHT),
Freq("None"),
Weight("None"),
Link to original data table(0),
output table name("Main Table Summary")
);
//close unnecessary datatable
Close(dt, no save);
//add new column with formula
dt_summary << New Column("Percent Missing",
Numeric,
"Continuous",
Format("Percent", 12, 0),
Formula(:Name("N Missing(HEIGHT)") / :"N Rows"),
);
//rename columns
Column(dt_summary, "N Rows") << Set Name("Total No of Rows");
Column(dt_summary, "N Missing(HEIGHT)") << Set Name("No of Rows with Missing Data");
回复:JSL - 查找缺失數據的百分比你好@rverma ,
我發現您可以利用表格中的摘要。我可以生成您在該部分中所做的大部分內容,然後添加一個包含公式的列來計算比率(%)。
我可以製作這樣的腳本,如果沒有丟失數據,它仍然可以工作(地址第 2 項):
sumdt = dt << Summary( Group( :ID, :SUB_ID ), N Missing( :HEIGHT ), Freq( "None" ), Weight( "None" ) );
sumdt << New Column( "Percent Missing",
Numeric,
Continuous,
Format( Percent ),
Formula( :"N Missing(HEIGHT)"n / :N Rows )
);
然後你可以對此進行子集化。 並且仍然關閉其他窗口。
這是一個開始