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 )
);
然后你可以对此进行子集化。 并且仍然关闭其他窗口。
这是一个开始