從文件存儲變量並在 jsl 腳本中使用 for max 函數你好成員,
我是剛開始編寫 JSL 腳本的新用戶,您能否提供如何執行以下需要的建議
我有 2 個文件,
File1 :包含具有數千列(字符串)和行(數字)的數據
File2:包含一些我手動創建的列列表,用於從 file1 中選擇感興趣的列
我需要根據文件 2 中的列表執行 max 函數並在文件 1 中創建新列
下面是我嘗試過但沒有按預期工作的代碼。
dt = 打開 ("C:/File2.csv")
列表 = dt << 獲取名稱; // list = 基本上是來自 file1 = row 1、row2、row10 等的幾個隨機列名
dt1 = 打開(“C:/File1.jmp”)
新列 ("max list",Numeric,Formula (Max (list)));
數據表腳本回复:從文件存儲變量並在 jsl 腳本中使用 for max 函數不確定您的數據是什麼樣的,但這是一個建議。 不是最簡單的解決方案,需要 JMP16+
Names Default To Here(1);
dt = New Table("File2",
Add Rows(0),
Compress File When Saved(1),
New Column("Column 1", Character, "Nominal", Format("Best", 12), Set Values({"Column 1", "Column 2"}))
);
dt1 = New Table("File1",
Add Rows(3),
Compress File When Saved(1),
New Column("Column 1", Numeric, "Continuous", Format("Best", 12), Set Values([1, 1, 1])),
New Column("Column 2", Numeric, "Continuous", Format("Best", 12), Set Values([2, 2, 2])),
New Column("Column 3", Numeric, "Continuous", Format("Best", 12), Set Values([3, 3, 3]))
);
Show(dt << Get Name); // this will get name of the datatable not columns
max_list = Column(dt, "Column 1") << get values;
expr_max = Expr(Max());
For Each({col}, max_list,
Insert Into(expr_max, NameExpr(AsColumn(Column(dt1, col))))
);
Show(NameExpr(expr_max));
Eval(EvalExpr(
dt1 << New Column("MaxList", Numeric, Continuous, Formula(
Expr(NameExpr(expr_max))
));
));
使用 Eval Insert、Eval Expr、Parse 和 Substitute 將一個表達式插入另一個表達式為了好好讀書
回覆:從檔案儲存變數並在 jsl 腳本中使用 max 函數非常感謝你,特克斯尼爾森,
有效 。犯了一個簡單的錯誤,沒有在 file2 中使用列名 Maxlist
但我收到另一個錯誤:名稱空間「office1.close:today」未定義傳回參數中的最大值或清單參數的單一矩陣中的值
「office1.close:today」是 file2 之一
如果我打開 file1 maxlist 列並點擊編輯公式,錯誤就會消失。
回覆:從檔案儲存變數並在 jsl 腳本中使用 max 函數我再次查看了您最新的帖子,我相信我有一個新腳本可以解決您在該帖子中暴露的複雜名稱問題。
// The code below this line is the that does the calculations The code above this line would
// not normally be included
Names Default To Here( 1 );
// point to the large data table with lots of rows and columns
dt = Data Table( "Main" );
// If you are opening the table from a folder, this statement would be
// dt = open("the path to the table");
// point to the second data table that contains a list of columns to be summed
dtMax = Data Table( "config" );
// If you are opening the table from a folder, this statement would be
// dtMax = open("the path to the table");
// Just added a short pause so one can see the original tables before changing
// Create the new columns
For( i = 1, i <= N Cols( dtMax ), i++,
sumList = Column( dtMax, i ) << get values;
dt << New Column( Column( dtMax, i ) << get name );
nameList = ":\!"" || sumList[1] || "\!"n";
For( k = 2, k <= N Rows( dtMax ), k++,
Insert Into( nameList, ", :\!"" || sumList[k] || "\!"n" )
);
Column( dt, N Cols( dt ) ) << set formula( Eval( Parse( "max(" || nameList || ")" ) ) );
);回覆:從檔案儲存變數並在 jsl 腳本中使用 max 函數
感謝您的建議,它沒有按預期工作,我可能做錯了什麼。為了更清楚起見,我提供了文件的外觀和要求
file1 看起來像這樣(這是我的主表,我想在其中找到 office1,2,4 的最大值)
| 辦公室1 | 辦公室2 | 辦公室3 | 辦公室4 | 辦公室5 |
| 2 | 3 | 0 | 6 | 1 |
| 3 | 2 | 6 | 4 | 4 |
| 2 | 5 | 3 | 2 | 3 |
| 1 | 0 | 2 | 3 | 2 |
| 4 | 1 | 2 | 7 | 1 |
| | | | | |
新列Maxlist 的預期輸出(來自 office1、2、4 的值)
簡單的解決方案: New Column("MaxList", Numeric, Continuous, Formula(Max(:"office1",:"office2,:"office4")));
| 辦公室1 | 辦公室2 | 辦公室3 | 辦公室4 | 辦公室5 | .. | 最大列表 | | |
| 2 | 3 | 0 | 6 | 1 | | 6 | | |
| 3 | 2 | 6 | 4 | 4 | | 4 | | |
| 2 | 5 | 3 | 2 | 3 | | 5 | | |
| 1 | 0 | 2 | 3 | 2 | | 3 | | |
| 4 | 1 | 2 | 7 | 1 | | 7 | | |
| | | | | | | | | |
由於我的 maxlist 每天都會變化,我想使用另一個 file2 作為 max list 的輸入參數
file2:作為輸入參數給出的列名稱清單並執行腳本
尋找解決方案,我可以在 file2 中給出最大列表並在 file1 中執行
New Column("MaxList", Numeric, Continuous, Formula(Max(基於file2 列表")));