<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />你好
我想尋求一些有關 JMP 腳本的幫助。
我需要從當前表中自動檢索多個列名,並使用公式為每一列創建一個新列。
例如,這是我的桌子
單元 ID 數據 1 數據 2 數據 3
1.1 1.2 1.1
B 1.2 1.2 1.0
C 1.3 1.2 1.4
D 1.1 1.2 1.1
1 - 我需要選擇從 Data1 到 Data3 的列
2 - 需要創建 3 個新列,如下 Data1_Floor、Data2_Floor、Data3_Floor
3 - 每個新列都將使用原始列的數據計算為“Floor”函數:Data1_Floor = Floor (Data1), ...
我將不勝感激帶有一些註釋的 JSL 腳本示例
謝謝
亞歷克斯
這應該這樣做:
dt = current data table();
startcol = 2;
endcol = 4;
for (i = startcol, i <= endcol, i++,
data_col = column(i) << get name;
floor_col = data_col || "_Floor";
str = evalinsert(
"\[dt << new column( "^floor_col^", Numeric, Continuous, Format( "Best", 12 ),
formula(floor(:^data_col^)));]\");
eval(parse(str));
);
我還要問你:如果我的列的名稱中沒有公共字符串怎麼辦?我怎樣才能讓它工作,而不是“ifcontains”,我必須告訴腳本將列從 B 列到 D 列,或者按數字順序,從#2 到#4 列?
你很接近。您需要分配變量dt指向您的數據表。你可以這樣做:
// Replace <YOUR table="" name=""> with the actual name of your table
dt = data table("<YOUR table="" name="">");
or with:
dt = current data table();</YOUR></YOUR>
嗨,PMroz,
感謝您的快速回复,我很感激。不幸的是,我是 JSL 的新手,所以我無法重現它,我嘗試從“ col_list = ...”但我沒有得到預期的輸出。我一定做錯了什麼。
這是我想要得到的。
這是我的原始數據表:
| 單元 | 數據 1 | 數據 2 | 數據 3 |
| A | 12.35336 | 8.876483 | 3.207919 |
| 乙 | 9.838289 | 1.483974 | 2.5232 |
| C | 3.136643 | 6.092754 | 0.980194 |
| 丁 | 0.3593 | 4.639877 | 6.672549 |
我想運行腳本,該腳本將獲取所有帶有“Data”字符串的列,並且每個列都將生成新列,因此最終表將如下所示:
| 單元 | 數據 1 | 數據 2 | 數據 3 | 數據_1樓 | 數據_2樓 | 數據_3樓 |
| A | 12.35336452 | 8.876483446 | 3.207918972 | 12 | 8個 | 3個 |
| 乙 | 9.838288756 | 1.483974287 | 2.523200042 | 9 | 1個 | 2個 |
| C | 3.136642653 | 6.092754288 | 0.980193636 | 3個 | 6個 | 0 |
| 丁 | 0.359299803 | 4.639877062 | 6.672548555 | 0 | 4個 | 6個 |
你能嘗試製作一個腳本來完成這個動作嗎?
// Create data table
dt = New Table( "Floor Example",
Add Rows( 4 ),
New Column( "Unit ID", Character, Nominal, Set Values( {"A", "B", "C", "D"} ) ),
New Column( "Data1", Numeric, Continuous, Format( "Best", 12 ),
Set Values( [1.1, 1.2, 1.3, 1.1] ) ),
New Column( "Data2", Numeric, Continuous, Format( "Best", 12 ),
Set Values( [1.2, 1.2, 1.2, 1.2] ) ),
New Column( "Data3", Numeric, Continuous, Format( "Best", 12 ),
Set Values( [1.1, 1, 1.4, 1.1] ) ),
);
// Get the list of column names
col_list = dt << get column names(string);
// Loop over column names, searching for data columns
for (i = 1, i <= nitems(col_list), i++,
// If it's a Data column then add the floor formula
if (contains(col_list[i], "Data"),
data_col = ":" || col_list[i];
floor_col = col_list[i] || "_Floor";
// Build new column string dynamically and then execute it.
str = evalinsert(
"\[dt << New Column( "^floor_col^", Numeric, Continuous, Format( "Best", 12 ),
Formula( Floor( ^data_col^))
);]\");
eval(parse(str));
);
);