简体   繁体   English

r相关图中的对角线替换

[英]diagonal replacement in an r correlogram

I am rather new to R. I am trying to replace the main diagonal of a correlogram (that's consisted of ones obviously). 我对R相当陌生。我正在尝试替换相关图的主要对角线(显然是由对角线组成)。 I have created the vectors for the the correlogram, and have used the cor() function from the cocron package to create the correlogram. 我已经为相关图创建了向量,并使用了cocron包中的cor()函数来创建相关图。 I also created a list with the values that i want instead of the ones in the correlogram, consisted of internal reliabilities of the correlogram vectors. 我还创建了一个包含所需值的列表,而不是相关图中的值,该值由相关图矢量的内部可靠性组成。

library(cocron)
library(fmsb)
# defining correlated variables
JOB_ins = subset(df,select=c("q9","Rq10_new","q11","q12"))
INT_to_quit = subset(df,select=c("q13","q14","Rq15_new","q16"))
Employability = subset(df,select=c("q17","q18","q19","q20"))
Mobility_pref = subset(df,select=c("Rq21","Rq22","Rq23","Rq24","Rq25"))
Career_self_mgmt = subset(df,select=c("q26","q27","q28","q29","q30"
                                         ,"q31","q32","q33"))


# subsetting dataframes 
x = subset(df,select=c(JOB_ins, INT_to_quit, Employability
                                          ,Mobility_pref,Career_self_mgmt))

#creating a correlation matrix

corrmat = cor(x)

#creating Cronbach Alpha reliabilities vector for diagonal replacement
dlist=list(round(CronbachAlpha(JOB_ins),2),round(CronbachAlpha(Int_to_quit),2)
                           ,round(CronbachAlpha(Employability),2)
                           ,round(CronbachAlpha(Mobility_pref),2)
                           ,round(CronbachAlpha(Career_self_mgmt),2))
#replacing the main diagonal
diag(corrmat)=dlist

Doing that I do replace the main diagonal but It seems I also turn my correlogram from a matrix to a vector. 这样做我确实替换了主对角线,但似乎我也将相关图从矩阵转换为向量。 Any idea how do I keep that from happening or reverse that? 知道如何防止这种情况发生或扭转这种情况吗?

First, you can use a vector instead of a list, replace list(round(CronbachAlpha(JOB_ins),2),...) by c(round(CronbachAlpha(JOB_ins),2),...) 首先,您可以使用向量而不是列表,将list(round(CronbachAlpha(JOB_ins),2),...)替换为c(round(CronbachAlpha(JOB_ins),2),...)

Second, you can convert a vector to a matrix easily. 其次,您可以轻松地将向量转换为矩阵。 Example: matrix(c(1,2,3,4), nrow = 2) will convert the c(1,2,3,4) vector into the following 2x2 matrix: 示例: matrix(c(1,2,3,4), nrow = 2)会将c(1,2,3,4)向量转换为以下2x2矩阵:

     [,1] [,2]
[1,]    1    3
[2,]    2    4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM