I want to create a single column that lets me know the correlations for my dependent variable with all of the explanatory variables that I am interested in (all these columns and many more are stored in a data.frame d). By doing cor(d) I can get all the correlations and开发者_高级运维 by doing cor(d$Var1, d$Var2) I can get a single number, but I want to figure out how to get only the Var1 column from the matrix returned by cor(d), with my being able to select the explanatory variables I want included.
The cor function can actually do this as well. Suppose we have:
d=data.frame(dependentVar = c(1,2,3),var1=c(-1,-2,-3),var2=c(9,0,5),junk=c(-2,-3,5))
Then this will do the trick:
cor(d[,"dependentVar"], d[,c("var1","var2")])
var1 var2
[1,] -1 -0.4435328
It's less efficient (I guess), but you can also do this:
cor(d)["dependentVar", c("var1","var2")]
which computes the full correlation matrix, and then pulls out the subset you want.
@DavidR is correct, though R also supports correlation between the columns of X and columns of Y as:
cor(X, Y)
See ?cor
for more information.
M <- round(cor(College[,-1]),2)
library('corrplot') #package corrplot
We can use circle, square, eclipse also instead of number if we want images instead of direct numbers
corrplot(M, method = "number") #plot matrix
Corelation matrix
精彩评论