矩阵代码简化和概括
祝美好的一天,请我需要帮助,以试图对任何矩阵进行概括性数据政治民主在 lavaan 库中都可以使用,
我尝试了
library(lavaan)
R<-chol(cor(PoliticalDemocracy[1:8]))
diag(R)<-NA
R<-round(R,3)
R
R[R==0]<-NA
R
d<-matrix(NA,nrow(R),ncol(R))
d[,1]<-acos(R[1,])
d
d[,2]<-acos(R[2,]/sin(d[,1]))
d[,3]<-acos(R[3,]/sin(d[,1])*sin(d[,2]))
d[,4]<-acos(R[4,]/sin(d[,1])*sin(d[,2])*sin(d[,3]))
d[,5]<-acos(R[5,]/sin(d[,1])*sin(d[,2])*sin(d[,3])*sin(d[,4]))
d[,6]<-acos(R[6,]/sin(d[,1])*sin(d[,2])*sin(d[,3])*sin(d[,4])*sin(d[,5]))
d[,7]<-acos(R[7,]/sin(d[,1])*sin(d[,2])*sin(d[,3])*sin(d[,4])*sin(d[,5])*sin(d[,6]))
d[,8]<-acos(R[8,]/sin(d[,1])*sin(d[,2])*sin(d[,3])*sin(d[,4])*sin(d[,5])*sin(d[,6])*sin(d[,7]))
我尝试使用循环但有错误的方法和一个空矩阵D
d<-matrix(NA,nrow(R),ncol(R))
d[,1]<-acos(R[1,])
d
for (i in 2:nrow(d)){
d[,i]<-acos(R[i,])/prod(sin(d[,i-1]))
d
}
d
@allancameron
Good day, Please i need assistance trying to generalize this code for any matrix the data PoliticalDemocracy is available in the lavaan library
What I tried
library(lavaan)
R<-chol(cor(PoliticalDemocracy[1:8]))
diag(R)<-NA
R<-round(R,3)
R
R[R==0]<-NA
R
d<-matrix(NA,nrow(R),ncol(R))
d[,1]<-acos(R[1,])
d
d[,2]<-acos(R[2,]/sin(d[,1]))
d[,3]<-acos(R[3,]/sin(d[,1])*sin(d[,2]))
d[,4]<-acos(R[4,]/sin(d[,1])*sin(d[,2])*sin(d[,3]))
d[,5]<-acos(R[5,]/sin(d[,1])*sin(d[,2])*sin(d[,3])*sin(d[,4]))
d[,6]<-acos(R[6,]/sin(d[,1])*sin(d[,2])*sin(d[,3])*sin(d[,4])*sin(d[,5]))
d[,7]<-acos(R[7,]/sin(d[,1])*sin(d[,2])*sin(d[,3])*sin(d[,4])*sin(d[,5])*sin(d[,6]))
d[,8]<-acos(R[8,]/sin(d[,1])*sin(d[,2])*sin(d[,3])*sin(d[,4])*sin(d[,5])*sin(d[,6])*sin(d[,7]))
I tried using the loop but was having errors and an empty matrix d
d<-matrix(NA,nrow(R),ncol(R))
d[,1]<-acos(R[1,])
d
for (i in 2:nrow(d)){
d[,i]<-acos(R[i,])/prod(sin(d[,i-1]))
d
}
d
@AllanCameron
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
RELAD
函数,如下所示You could use
Reduce
function as shown below