如何將公式套用到另一個表中的所有欄位?我正在嘗試獲取一個表格並將移動平均值應用於每列的每個函數。下面的腳本是我正在嘗試的,基於我在另一個線程中看到的內容。目標是建立一個新表,然後在引用原始表的同時套用該函數。但是,我不斷收到範圍資料表存取錯誤。
Test = Open("C:\Users\abrokaw\Desktop\JMP MA Test Table.jmp"); //open value data table
Testcols = Test << Get Column Names( string ); //get column names
TestMA = New Table( "Test Table Smoothed" ); //make new table
TestMA << add rows(nrows(Test)); //make new table match rows with raw table
For( i = 1, i <= N Cols( Test ), i++,
Eval(
Parse(
"TestMA << New Column( \!"MA_" || Testcols[i] || "\!",
numeric,
continuous,
formula( test:" ||
Testcols[i] || "Col Moving Average( :Value, 1, 2, 2 ) ) )'"
)
)
);
TestMA << delete columns("column 1");
自動化和腳本編寫基礎資料分析與建模回覆:如何將公式套用到另一個表格中的所有欄位?您將對測試表中的列的參考放置在錯誤的位置。另外,我從使用格式更改為使用設定每個值。最終的計算結果是相同的,但該列不是公式列。這導致新數據表獨立於測試表。我還更改了 For() 迴圈的結束值。由於前後均包含 2 行,因此無法計算超出 nrows-2
names default to here(1);
Test = Open( "$SAMPLE_DATA/blood pressure.jmp" ); //open value data table
Testcols = Test << Get Column Names( string, continuous ); //get column names
TestMA = New Table( "Test Table Smoothed" ); //make new table
TestMA << add rows( N Rows( Test ) ); //make new table match rows with raw table
For( i = 1, i <= N Cols( Test )-2, i++,
Eval(
Parse(
"TestMA << New Column( \!"MA_" || Testcols[i] ||
"\!",
numeric,
continuous,
set each value( Col Moving Average( test:" || Testcols[i] || ", 1, 2, 2 ) ) )"
)
);
);
TestMA << delete columns( "column 1" );
最後,請使用預覽畫面頂部的 JSL 圖示輸入您的程式碼。它使閱讀變得更加容易。
該程式碼實際上不需要 Eval(Parse()) 程式碼的複雜性。它可以只使用開放代碼
Names Default To Here( 1 );
Test = Open( "$SAMPLE_DATA/blood pressure.jmp" ); //open value data table
Testcols = Test << Get Column Names( string, continuous ); //get column names
TestMA = New Table( "Test Table Smoothed" ); //make new table
TestMA << add rows( N Rows( Test ) ); //make new table match rows with raw table
For( i = 1, i <= N Cols( Test ) - 2, i++,
TestMA << New Column( "MA_" || Testcols[i],
numeric,
continuous,
set each value( Col Moving Average( test:Testcols[i], 1, 2, 2 ) )
)
);
TestMA << delete columns( "column 1" );
回覆:如何將公式套用到另一個表格中的所有欄位?您也可以使用 Subset 來複製列(這也會複製表格腳本等,這可能是一個好處,也可能是一個問題)。如果您使用子集選項並使用公式列進行計算,請記住使用複製公式(0)
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
cont_cols = dt << Get Column Names(string, continuous);
ma_cols = {};
For(i = 1, i <= N Items(cont_cols), i++, // could use For Each but I try to avoid ... Each loops nowadays
//Eval(EvalExpr()) isn't necessary here but makes debugghing much easier
new_col = Eval(EvalExpr(
dt << New Column("MA_" || cont_cols[i], Numeric, Continuous, Formula( // I also try to avoid Set Each Value in cases like this so I use Formula here
Col Moving Average(Expr(NameExpr(AsColumn(dt, cont_cols[i]))), 1, 2, 2)
));
));
Insert Into(ma_cols, new_col << get name);
);
dt_new = dt << Subset(All Rows, Columns(ma_cols), Copy Formula(0), Output Table("Moving Averages"));
dt << Delete Columns(ma_cols);回覆:如何將公式套用到另一個表格中的所有欄位?謝謝,吉姆。此更改適用於我製作的測試資料集。但是,我在真實資料集上仍然遇到了相同錯誤訊息的問題。我認為這與列名稱有關,因為當我將列名稱從真實資料集複製到測試集時,我也遇到了相同的錯誤。列名稱中是否存在可能導致此問題的某些字元?