概述
使用 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 中存取該指南。從線上目錄中,選擇Scripting Guide 。
[以前的 JMP 註 46535]
替代方案1:使用表格/更新
差異:使用r=ageRange[Length(agerange)]而不是agerange[1]
替代方案2:使用表格/連接
區別:產生一個新表