替換矩陣值你好! 我正在嘗試按順序將所有列資料輸入到 8 x 12 矩陣中(首先以向下的方式填充列)。 我的列中只有 72 個值,因此我希望剩餘未填入的值均為 0。我在嘗試讓下面的程式碼工作時遇到很多問題。 我將不勝感激任何幫助!
dt = Open("Example.jmp");<BR />Rows = nrows(dt);
names= dt << Get Column Names (String);
Table =[];
Table = J(8,12,0);
For(i=1, i <= ncol(dt),i++,
data = column(dt, names[i]) <<GETVALUES></GETVALUES>回覆:替換矩陣值謝謝你!這很好用回覆:替換矩陣值我沒有您的範例,因此我不得不使用 JMP 範例資料表之一。 將初始資料表的這些行替換為您自己的行。
Names Default to Here( 1 );
// use sample data to illustrate
dt = Open( "$SAMPLE_DATA/Fitness.jmp" );
// delete non-numeric data columns
dt << Delete Columns( { :Name, :Sex } );
// make empty container
table = Associative Array();
// get each data column and create (key, value) pair (key = col name, value = 8x12 matrix)
For( c = 1, c <= N Col( dt ), c++,
data = Column( dt, c ) << Get As Matrix;
If( N Row( data ) < 96,
data |/= J( 96 - N Row( data ), 1, 0 );
);
table[Column( dt, c ) << Get Name] = Transpose( Shape( data, 12, 8 ) );
);
Close( dt, No Save );
dir = Get Default Directory();
content = table << Get Contents;
For( t = 1, t <= N Items( content ), t++,
dt = As Table( content[t][2] );
file path = dir || content[t][1] || ".CSV";
dt << Save( file path );
Close( dt, No Save );
text = Load Text File( file path );
eol = Contains( text, "\!n" );
text = Munger( text, 1, eol - 1, content[t][1] );
Save Text File( file path, text );
);回覆:替換矩陣值非常感謝你的幫忙。 我很感激。 我能夠使用您的程式碼保存文件,但目前 csv 文件的第一行是資料的第一行,沒有列名稱。 我想讓第一個單元格具有列名稱,並在其正下方顯示 8 x 12 資料。回覆:替換矩陣值請耐心等待。 由於某些未知原因,我在儲存 CSV 檔案時遇到困難。 我正在與技術支援部門合作。
當我等待他們回覆時,我想確保您想要將 CSV 檔案的第一行(原始資料列標題)替換為原始資料列名稱。
回覆:替換矩陣值這有效! 但是,我實際上想將 csv 檔案中的列名稱(或在本例中為 content[t][1])保留為第一行。 目前還沒有顯示。 你知道我如何將它保留在 csv 檔案的第一個儲存格中嗎?回覆:替換矩陣值好的,這個更新版本的腳本應該可以解決問題:
Names Default to Here( 1 );
dt = Open( "Example.jmp" );
table = Associative Array();
For( c = 1, c <= N Col( dt ), c++,
data = Column( dt, c ) << Get As Matrix;
If( N Row( data ) < 96,
data |/= J( 96 - N Row( data ), 1, 0 );
);
table[Column( dt, c ) << Get Name] = Transpose( Shape( data, 12, 8 ) );
);
content = table << Get Contents;
For( t = 1, t <= N Items( content ), t++,
dt = As Table( content[t][2] );
Close( dt, Save( content[t][1] || ".CSV" ) );
);
不幸的是,我不知道如何隱藏 CSV 檔案中帶有標題名稱的第一行。 也許其他人知道。 我們可以立即以文字形式讀回文件,刪除第一行,然後再次儲存。
回覆:替換矩陣值我需要我的設定的佈局,並且 csv 文件是設備讀取的唯一文件類型回覆:替換矩陣值請參閱說明 > 腳本索引。 點擊下拉按鈕並選擇功能。 搜尋 As Table() 函數。
回覆:替換矩陣值我懷疑作為新的 JMP 用戶,您的思維方式可能仍然是電子表格用戶的思維方式。 我不知道 CSV 檔案的目標,但您可以使用高表格佈局在 JMP 中做很多事情。
為什麼需要 8x12 佈局? 為什麼需要將矩陣匯出為 CSV 檔案?
如果我們知道此分析的進展情況,我們也許能夠幫助您利用 JMP 發揮優勢。
回覆:替換矩陣值請看一下這個帖子。 您可能會發現它在平板實驗工作中很有用。
回覆:替換矩陣值我可能有點多餘,但鑑於需要輸出到 csv 文件,這裡有另一種處理此問題的方法。它使用資料表操作將資料轉換為正確的形式,然後將資料表寫入 csv 文件
Names Default To Here( 1 );
// Create some sample data
dt = New Table( "Example",
add rows( 72 ),
New Column( "c1", formula( Random Uniform( 1, 500 ) ) ),
New Column( "c2", formula( Random Uniform( 1, 500 ) ) ),
New Column( "c3", formula( Random Uniform( 1, 500 ) ) )
);
dt << run formulas;
dt:c1 << delete property( "formula" );
dt:c2 << delete property( "formula" );
dt:c3 << delete property( "formula" );
// Get the list of columns
colNamesList = dt << get column names( string, numeric );
// Loop across the columns and process them
For( i = 1, i <= N Cols( dt ), i++,
// Create a new table will only the single column
dtTemp = dt << subset(invisible, selected rows( 0 ), columns( Column( colNamesList[i] ) ) );
// If there are not 96 rows, add them
If( N Rows( dtTemp ) < 96,
rowCount = N Rows( dt );
dtTemp << add rows( 96 - rowCount );
For( k = rowCount + 1, K <= 96, k++,
Column( dtTemp, colNamesList[i] )[k] = 0
);
);
// Create a sequence column to allow for proper splitting of the data
dtTemp << New Column( "sequence",
formula(
x = Mod( Row(), 8 );
If( x == 0, x = 8 );
x;
)
);
dtTemp << run formulas;
// Split the data into 8 columns.....if this should be 12 just change the sequence formula above
dtSplit = dtTemp << Split(invisible, Split By( :sequence ), Split( Column( dtTemp, colNamesList[i] ) ), Sort by Column Property );
dtSplit << run formulas;
// Close the un needed temporary data table
Close( dtTemp, nosave );
// Change the output preferences to get rid of headers
current_pref = Char( Arg( Parse( (Char( Get Preferences( Export settings ) )) ), 1 ) );
// Set prefs (comma delimited, no headers)
Pref( Export Settings( End Of Field( Comma ), Export Table Headers( 0 ) ) );
// Save csv file
dtSplit << save( "$TEMP\" || colNamesList[i] || ".csv" );
//Restore oribinal prefs
Eval( Parse( "pref(" || current_pref || ")" ) );
// Close the un necessary table
Close( dtSplit, nosave );
);
回覆:替換矩陣值感謝您的幫助和興趣。 馬克貝利的關聯數組方法已經奏效。 現在我試圖弄清楚如何將關聯數組放入資料表中,以便可以將其匯出為 CSV 檔案。回覆:替換矩陣值因此,一旦將資料放入矩陣中,如何將它們移動到永久位置?回覆:替換矩陣值對於造成的混亂,我深感抱歉。 我試圖將每個列資料儲存為 8 x 12 的格式(這是具有 96 孔的微量滴定板的大小),以便儲存與其在板上的位置相關的實驗結果。 每列代表一個不同的實驗。回覆:替換矩陣值我從一開始就一直在關注這個討論。它似乎已經演變成一個腳本練習,而沒有暴露真正的最終目標是什麼。 @馬克貝利透過使用關聯數組提供了一個很好的解決方案,但我認為缺少的是創建這些矩陣的真正原因是什麼?如果知道這一點,可能會有一個更簡單的解決方案。
回覆:替換矩陣值列名儲存在關聯數組中。 他們是鑰匙。 您可以透過將以下內容新增至腳本來驗證此聲明:
Show( table << Get Keys );
我建議您查看“幫助”>“書籍”>“腳本指南”,然後搜尋“關聯數組”。 您可以迭代這些對並輕鬆執行許多其他任務。 指南中的範例很好,但如果您有任何疑問,請回到此處。
回覆:替換矩陣值謝謝你!! 這效果很好。 但是,不儲存列名稱。 你知道我該如何解決這個問題嗎?回覆:替換矩陣值您的資料列是微量滴定板的結果嗎?
您想要將每列中的資料重新調整為 8x12 表。 您的腳本將僅保留最後一列的表格。 這是一個處理每個資料列並將表結果與列名儲存在關聯數組中的腳本。
Names Default to Here( 1 );
dt = Open( "Example.jmp" );
table = Associative Array();
For( c = 1, c <= N Col( dt ), c++,
data = Column( dt, c ) << Get As Matrix;
data[Loc(Is Missing( data ))] = 0;
table[Column( dt, c ) << Get Name] = Transpose( Shape( data, 12, 8 ) );
);
Show( table << Get Contents );
我假設您至少有 96 行。 如果有更多,它們就會丟失。 如果較少,則應將 0 的分配變更為 0 填充的串聯。 這個版本改為:
Names Default to Here( 1 );
dt = Open( "Example.jmp" );
table = Associative Array();
For( c = 1, c <= N Col( dt ), c++,
data = Column( dt, c ) << Get As Matrix;
If( N Row( data ) < 96,
data |/= J( 96 - N Row( data ), 1, 0 );
)
table[Column( dt, c ) << Get Name] = Transpose( Shape( data, 12, 8 ) );
);
Show( table << Get Contents );
回覆:替換矩陣值感謝您的幫忙! 我可以使用 Get As Matrix 函數將資料放入指定的矩陣中嗎? 我需要將列中的資料放入 8 x 12 矩陣中。回覆:替換矩陣值感謝吉姆的回覆!資料表現在有 5 列,但它可以根據使用者輸入進行更改。 我基本上試圖將資料列放入 12 列 8 行的矩陣中。
(a - 1) * b + b 似乎對我不起作用,我猜是因為對於第一列數據,它始終是 data[0]。 你知道出了什麼問題嗎?回覆:替換矩陣值吉姆提供了一個非常好的解決方案,但我也不確定你想要做什麼,所以我提供另一種方式來說明另一點。
Names Default to Here( 1 );
// open example
dt1 = Open( "$SAMPLE_DATA/Big Class.jmp" );
// make illustrative example with colums of different sizes (missing values)
dt2 = dt1 << Split(
Split By( :age ),
Split( :weight ),
Remaining Columns( Drop All ),
Sort by Column Property
);
Close( dt1, No Save );
// input data table as matrix
mat = dt2 << Get As Matrix;
// replace missing values with zero
mat[Loc(Is Missing(mat))] = 0;
// see result
Show( mat );
希望能幫助你。
回覆:替換矩陣值我真的不確定我明白你想做什麼。但下面是一個腳本,它將在名為 data 的矩陣用完值後填入零
dt = Open( "Example.jmp" );
Rows = N Rows( dt );
names = dt << Get Column Names( String );
Table = [];
Table = J( 8, 12, 0 );
For( i = 1, i <= N Col( dt ), i++,
data = Column( dt, names[i] ) << getValues;
For( a = 1, a <= 12, a++,
For( b = 1, b <= 8, b++,
// I believe that (a - 1 ) * b + b will be equal to the
// row number of the data matrix you read in. Once it's
// value is greater than the number of rows in the dt data
// table, it will enter zeros.
If( (a - 1) * b + b <= Rows,
Table[a, b] = data[(a - 1) * b + b],
Table[a, b] = 0
)
)
);
);
Print( Table );
我對您在外部 For() 循環中從一列到另一列所做的事情感到困惑。資料表Example中有多少列?