複数の CSV ファイルを介してスクリプトを循環させる方法前回の投稿に続き、扱いにくい CSV ファイルをスタックしてフォーマットするスクリプトを作成しました。
https://community.jmp.com/t5/Discussions/Help-doing-a-table-transform-and-converting-a-timestamp/td-p/303091
次の質問は、フォルダー内の CSV のグループに対してこれを繰り返し、ファイル名を列の 1 つとしてキャプチャし、理想的にはそれらをスタックする方法を教えてください。別名 step1、step2、step3、step4 ファイル。
単一の CSV をスタックすると、400k 行の出力が作成されます。 これらの CSV を 4 つ、8 つ、または 10 つ結合するとクラッシュする危険がありますか?
おそらく、さらに、親フォルダー内の次のフォルダーを再度巡回して、新しい結合ステップ ファイルを作成することになるでしょうか。
names default to here(1);
//Prompt to select and open an MS Excel file
dt = Open( Pick File( "Select Excel File", "C:\Users\folder\", {"Excel Files|csv;xlsx;xls", "All Files|*"}, 1, 0 ), Invisible );
// Get the column names from the data table
colNames = dt << get column names;
// Remove the 1st col from the list
remove from(colNames,1,1);
// Stack the data
dtStack = dt << Stack(
columns(
colNames
),
Source Label Column( "Label" ),
Stacked Data Column( "Data" ),
Output Table( "Stacked_Data" )
);
//label the 1st column to Lamda
:Column 1 << Set Name("Lamda");
:Lamda << Modeling Type(nominal);
// Create a new JMP DateTime column
dtStack << New Column( "Timestamp",
Numeric,
"Continuous",
Format( "d/m/y h:m:s", 26, 3 ),
Input Format( "d/m/y h:m:s", 0 ),
Formula( Informat( Word( 1, :Label, "Z" ), "d/m/y h:m:s" ) )
);
// Remove formula to convert column to static values
dtStack:Timestamp << delete formula;
// Create the Time variable column
dtstack << New Column( "Time",
Numeric,
"Continuous",
Format( "hr:m:s", 17, 3 ),
Input Format( "hr:m:s", 0 ),
Formula( Time Of Day( :Timestamp ) )
);
//Create Absolute Time column, uses if>then error checking for Lamda group changing
dtstack << New Column( "AbsTime",
Numeric,
"Continuous",
Format( "Fixed Dec", 12, 1 ),
Formula(
If( Row() == 1 | :Lamda != Lag( :Lamda ),
0,
Lag( :AbsTime ) + (:Timestamp - Lag( :Timestamp ))
)
),
);
// Remove formula to convert column to static values
dtStack:AbsTime << delete formula;
// Get rid of no longer needed "Label" column
dtStack << delete columns( "Label" );
データのブレンディングとクリーンアップRe: 複数の CSV ファイルを使用してスクリプトを循環させる方法コードでは、ファイル名のリストに filename ではなく filelist を使用したいようです。 終わりに近いもの。
Re: 複数の CSV ファイルを使用してスクリプトを循環させる方法これを見ましたが、私のニーズには合わないようです。 個々の jmp dt を開くだけです。 列番号は、各プロセス ファイルの時間範囲に基づいて動的です。
Re: 複数の CSV ファイルを使用してスクリプトを循環させる方法素晴らしい。 動作しているように見えますが、最初のファイルの後にエラーが発生します。
「名前が解決されていません: filenam}1} 'filename' のアクセスまたは評価中、filename/*###*/」
何かご意見は?
また、フォルダー選択ポップアップを実行するようにスクリプトを調整しましたが、それは問題ではないと思います。
// Get a list of files from the directory
dir = Pick Directory( "Select a directory", "C:\Users\folder\");
fileList = Files In Directory( dir );
// Create a skeleton data table to end up being the combined data table
dtFinal = New Table("Final");
// Loop across all of the files found in the directory and read them in
For(i = 1, i <= N Items( fileList ), i++,
dt = open( dir || fileList[i] );Re: 複数の CSV ファイルを使用してスクリプトを循環させる方法複数ファイルのインポートもチェックしてください。 CSV ファイルをロードし、同じ列がある場合はそれらを連結し、オプションでファイル名列を追加できます。
Re: 複数の CSV ファイルを使用してスクリプトを循環させる方法基本的に、あなたがしなければならないことは、
- 「Files In Directory()」を使用して、処理するファイルのリストを取得します
- 最終データテーブル( dtFinal )に使用するデータテーブルを設定します。
- For() ループを使用して、ディレクトリ内にあるすべてのファイルをループして、deStack テーブルを作成します
- Source列をdtStackに追加します。
- dtStack を dtFinal テーブルに連結する
- dtStack テーブルと dt データ テーブルを閉じます。
以下は、上記の項目で変更されたスクリプトです。コードはテストされていないため、ファイルを読み取るディレクトリなどを追加する必要があります。最終製品を入手するための非常に良い出発点となるはずです
names default to here(1);
//Prompt to select and open an MS Excel file
//dt = Open( Pick File( "Select Excel File", "C:\Users\folder\", {"Excel Files|csv;xlsx;xls", "All Files|*"}, 1, 0 ), Invisible );
// Get a list of files from the directory
fileList = Files In Directory( <PATH to="" your="" directory=""> || fileList[i] );
// Get the column names from the data table
colNames = dt << get column names;
// Remove the 1st col from the list
remove from(colNames,1,1);
// Stack the data
dtStack = dt << Stack(
columns(
colNames
),
Source Label Column( "Label" ),
Stacked Data Column( "Data" ),
Output Table( "Stacked_Data" )
);
//label the 1st column to Lamda
:Column 1 << Set Name("Lamda");
:Lamda << Modeling Type(nominal);
// Create a new JMP DateTime column
dtStack << New Column( "Timestamp",
Numeric,
"Continuous",
Format( "d/m/y h:m:s", 26, 3 ),
Input Format( "d/m/y h:m:s", 0 ),
Formula( Informat( Word( 1, :Label, "Z" ), "d/m/y h:m:s" ) )
);
// Remove formula to convert column to static values
dtStack:Timestamp << delete formula;
// Create the Time variable column
dtstack << New Column( "Time",
Numeric,
"Continuous",
Format( "hr:m:s", 17, 3 ),
Input Format( "hr:m:s", 0 ),
Formula( Time Of Day( :Timestamp ) )
);
//Create Absolute Time column, uses if>then error checking for Lamda group changing
dtstack << New Column( "AbsTime",
Numeric,
"Continuous",
Format( "Fixed Dec", 12, 1 ),
Formula(
If( Row() == 1 | :Lamda != Lag( :Lamda ),
0,
Lag( :AbsTime ) + (:Timestamp - Lag( :Timestamp ))
)
),
);
// Remove formula to convert column to static values
dtStack:AbsTime << delete formula;
// Get rid of no longer needed "Label" column
dtStack << delete columns( "Label" );
// Add filename as a column to the data table
dtStack << new column( "Source", character, set each value( filename[i] ) );
// Concatenate to final table
dtFinal << concatenate( dtStack, append to first table(1) );
// Clean up
close( dtStack, nosave );
close( dt, nosave );
// End For() Loop
);</PATH>