Fill lower matrix with vector by row, not column

孤街醉人 提交于 2019-12-01 18:32:44

Just read it into the upper triangular portion, rather than the lower:

S <- diag(6)
S[upper.tri(S, diag=TRUE)] <- d
t(S)

The sem package has a very nice function, read.moments() that is designed to do just this:

foo <- read.moments()
 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

foo[upper.tri(foo)] <- t(foo)[upper.tri(foo)]

This gives you:

         X1       X2       X3       X4       X5       X6
X1  2.36750  0.86752 -0.36190 -0.32571 -0.37680 -0.37680
X2  0.86752  2.86750 -0.36190 -0.32571 -0.37680 -0.37680
X3 -0.36190 -0.36190  2.53810  0.84425  0.53136  0.53136
X4 -0.32571 -0.32571  0.84425  2.55980  0.47822  0.47822
X5 -0.37680 -0.37680  0.53136  0.47822  2.11200  0.91200
X6 -0.37680 -0.37680  0.53136  0.47822  0.91200  2.11200

EDIT1: As for the problem with scan(), just because it was originally printed as a lower triangle doesn't mean you have to put it in the lower triangle:) Just put it in the upper:

foo <- scan()
 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

bar <- matrix(0,6,6)

bar[upper.tri(bar,diag=TRUE)] <- foo

bar[lower.tri(bar)] <- t(bar)[lower.tri(bar)]

EDIT2: As for the problem with D notation, if I understand it correctly, can be fixed by first scanning characters, gsub the D to E and coerce as numeric:

foo <- scan(what="character")
 0.23675D+01  
 0.86752D+00  0.28675D+01 
-0.36190D+00 -0.36190D+00  0.25381D+01
-0.32571D+00 -0.32571D+00  0.84425D+00  0.25598D+01 
-0.37680D+00 -0.37680D+00  0.53136D+00  0.47822D+00  0.21120D+01 
-0.37680D+00 -0.37680D+00  0.53136D+00  0.47822D+00  0.91200D+00  0.21120D+01


bar <- matrix(0,6,6)

bar[upper.tri(bar,diag=TRUE)] <- as.numeric(gsub("D","E",foo))

bar[lower.tri(bar)] <- t(bar)[lower.tri(bar)]

bar
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!