Другой способ заключается в использовании библиотеки Hmisc и corrplot получить корреляции между всеми парами, значение и довольно сюжет, как так:
#Your data frame (4 variables instead of 10)
df<-data.frame(a=c(1:100),b=rpois(1:100,.2),c=rpois(1:100,.4),d=rpois(1:100,.8),e=2*c(1:100))
#setup
library(Hmisc)
library(corrplot)
df<-scale(df)# normalize the data frame. This will also convert the df to a matrix.
corr<-rcorr(df) # compute Pearson's (or spearman's corr) with rcorr from Hmisc package. I like rcorr as it allows to separately access the correlations, the # or observations and the p-value. ?rcorr is worth a read.
corr_r<-as.matrix(corr[[1]])# Access the correlation matrix.
corr_r[,1]# subset the correlation of "a" (=var1) with the rest if you want.
pval<-as.matrix(corr[[3]])# get the p-values
corrplot(corr_r,method="circle",type="lower",diag=FALSE,tl.col="black",tl.cex=1,tl.offset=0.1,tl.srt=45)# plot all pairs
corrplot(corr_r,p.mat = pval,sig.level=0.05,insig = "blank",method="circle",type="lower",diag=FALSE,tl.col="black",tl.cex=1,tl.offset=0.1,tl.srt=45)# plot pairs with significance cutoff defined by "p.mat"
Вы можете использовать применить заявление: 'применить (iris [, 2: 4], 2, function (x) cor (x, iris $ Sepal.Length)) ' –
Вы можете использовать' cor (data.frame) ', который даст вам матрицу корреляций между всеми переменными , Просто извлеките соответствующую строку/столбец из этой матрицы. – Sumedh
'cor (dat $ var1, dat [c (" var2 "," var3 "," var4 ")])'. Так, используя пример Филиппа, 'cor (iris $ Sepal.Length, iris [2: 4])' – user20650