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

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

但是在某些情況下,可能沒有缺少數據的行。 在這種情況下,“丟失百分比”應為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-查找丟失數據的百分比你好@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 )
);
然後,您可以對此子集。 並仍然關閉其他窗口。
它是一個開始
回复:JSL-查找丟失數據的百分比首先,我強烈建議您查看JMP腳本指南: JMP15腳本指南和腳本索引可從Jmp /幫助/腳本索引中找到。 這些是JSL腳本編寫的很好的資源。
要執行此任務,您將不需要任何jsl:
1.讓我們從為我們提供兩個缺失值的示例表開始
2.創建一個表的摘要,其中N Missing,高度為統計數據,SUB_ID和ID為組


3.通過選擇N行和N Missing(HEIGHT)列來創建新公式

4.將格式從“列屬性”更改為“百分比”,然後根據需要重命名列
基於這些步驟,您還可以輕鬆地使用JMP提供的功能來顯示將此手動流程更改為自動化流程所需的腳本(應進行一些小的更改以使其更可靠)。
對於您的問題1和3:
1.在很大程度上取決於如何創建/打開/獲取數據表。 如果您始終使用相同的名稱,則可以引用該名稱,但我不相信從長遠來看,因為創建具有相同名稱的多個數據表非常容易,並且JMP將在名稱之後添加索引。 但是這些可以由Datatable(nameofdatatable)引用。
3.您可以使用Get Data Table List()獲取數據表列表。 遍歷數據表並關閉不必要的數據表。 但是在我看來,應該始終使用Close(可能需要關閉數據表)來關閉不需要的數據表(對數據表的引用,不保存)。
執行任務的示例腳本:
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-查找丟失數據的百分比@jthi ,
我們正在同時思考,我希望您的回答比我的回答更加完整,並且同意第1項和第3項的建議。
回复: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 - 查找缺失數據的百分比如果您之前看到過,請重新閱讀。 有一個錯誤。
使用 JMP 的內置函數 Col N Missing ( ) 可以簡化此任務。 我將展示兩種方法。
解決方案#1:
- 創建一列以在第一個表中按組計算缺失。
- 總結第一張表。
- 更改 N Rows 列的名稱(如果需要)並為比率創建一個公式列。
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));