概述
使用 JMP 脚本语言 (JSL),您可以编写一个脚本,根据存储在不同数据表中的数据分配值。以下示例脚本演示了如何使用查找表根据学生的年龄分配学业水平。该脚本执行以下操作:
- 打开包含学生及其年龄列表的原始数据表。
- 在原始数据表中添加一个新列来存储学校值。
- 创建一个包含各学校年龄范围的查找表。
- 循环遍历原始数据表的每一行:
- 获取查找表中年龄属于年龄范围的行号。
- 将查找表行中的学校值分配给原始数据表中的新列。
指示
在 JMP 会话中,单击文件菜单并选择新建 ► 脚本。复制以下脚本并将其粘贴到脚本窗口中。要运行示例脚本,请单击编辑菜单并选择运行脚本。
/* Open a sample data table */
dt = Open( "$SAMPLE_DATA\Big Class.jmp" );
/* Add a column for the assignment */
dt << New Column( "School", Character );
/* Create a lookup table */
lt = New Table( "SchoolLookup",
Add Rows( 3 ),
New Column( "SchoolLevel", Character, Nominal, Set Values( {"Elementary", "Middle", "High"} ) ),
New Column( "Low", Numeric, Continuous, Format( "Best", 12 ), Set Values( [5, 13, 15] ) ),
New Column( "High", Numeric, Continuous, Format( "Best", 12 ), Set Values( [12, 14, 18] ) )
);
/* Loop through each row of the original data table */
For( i = 1, i <= N Rows( dt ), i++,
/* Obtain a matrix contining the row number of the lookup table where the age falls within the range */
ageRange = lt << Get Rows Where(
Column( lt, "Low" )[] <= Column( dt, "age" )[i] & Column( dt, "age" )[i] <= Column( lt, "High" )[]
);
/* Extract the row number from the matrix */
r = ageRange[1];
/* Assign the school value to the original data table */
Column( dt, "School" )[i] = Column( lt, "SchoolLevel" )[r];
);
其他文档
有关 JMP 脚本语言的更多信息,请参阅JMP 脚本指南。 您可以在 JMP 中单击“帮助”菜单并选择“JMP 帮助”来访问该指南。 从在线目录中,选择“脚本指南” 。
[先前的 JMP 说明 46535]
替代方案 1:使用 Tables/Update
区别:使用r= ageRange[Length(agerange)]而不是agerange[1]
替代方案 2:使用 Tables/Join
区别:生成一个新表