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));