按行而不是列用向量填充下矩阵 [英] Fill lower matrix with vector by row, not column
问题描述
我试图读取LISREL以下列格式在纯文本,空格分隔的文件中写出的方差-协方差矩阵:
I am trying to read in a variance-covariance matrix written out by LISREL in the following format in a plain text, whitespace separated file:
0.23675E+01 0.86752E+00 0.28675E+01 -0.36190E+00 -0.36190E+00 0.25381E+01
-0.32571E+00 -0.32571E+00 0.84425E+00 0.25598E+01 -0.37680E+00 -0.37680E+00
0.53136E+00 0.47822E+00 0.21120E+01 -0.37680E+00 -0.37680E+00 0.53136E+00
0.47822E+00 0.91200E+00 0.21120E+01
这实际上是一个较低的对角矩阵(包括对角线):
This is actually a lower diagonal matrix (including diagonal):
0.23675E+01
0.86752E+00 0.28675E+01
-0.36190E+00 -0.36190E+00 0.25381E+01
-0.32571E+00 -0.32571E+00 0.84425E+00 0.25598E+01
-0.37680E+00 -0.37680E+00 0.53136E+00 0.47822E+00 0.21120E+01
-0.37680E+00 -0.37680E+00 0.53136E+00 0.47822E+00 0.91200E+00 0.21120E+01
我可以使用scan()
或read.table(fill=T)
正确读取值.
I can read in the values correctly with scan()
or read.table(fill=T)
.
但是,我无法正确将读取的向量存储在矩阵中.以下代码
I am however not able to correctly store the read-in vector in a matrix. The following code
S <- diag(6)
S[lower.tri(S,diag=T)] <- d
按列填充下部矩阵,而应按行填充.
fills the lower matrix by column, while it should fill it by row.
使用matrix()
确实允许使用选项byrow=TRUE
,但这将填充整个矩阵,而不仅仅是下半部分(对角线).
Using matrix()
does allow for the option byrow=TRUE
, but this will fill in the whole matrix, not just the lower half (with diagonal).
是否可以同时使用这两种方法:仅填充较低的矩阵(对角线),并且按行执行?
Is it possible to have both: only fill the lower matrix (with diagonal) and do it by row?
(我遇到的另一个问题:LISREL使用'D + 01',而R仅将'E + 01'表示为科学计数法.您可以在R中将其更改为也接受'D'吗?)
(separate issue I'm having: LISREL uses 'D+01' while R only recognises 'E+01' for scientific notation. Can you change this in R to accept also 'D'?)
推荐答案
只需将其读入上方的三角形部分,而不是下方的部分:
Just read it into the upper triangular portion, rather than the lower:
S <- diag(6)
S[upper.tri(S, diag=TRUE)] <- d
t(S)
这篇关于按行而不是列用向量填充下矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!