替换矩阵值你好! 我正在尝试按顺序将所有列数据输入到 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中有多少列?