How to check correlation with R?

Correlation test is used to evaluate an association (dependence) between two variables. Correlation analysis can be performed using different methods. There are Pearson’s product-moment correlation coefficient, Kendall’s tau or Spearman’s rho. These methods are described in the following sections.
 
data for correlation test
Two variables, x and y, are used in the following examples:
 
x <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1)
y <- c( 2.6, 3.1, 2.5, 5.0, 3.6, 4.0, 5.2, 2.8, 3.8)
 
1.Pearson correlation coefficient
cor(x,y, method="pearson")
[1] 0.5712
 
The pearson correlation coefficient measure the linear dependence between two variables.
 
If method is “kendall” or “spearman”, Kendall’s tau or Spearman’s rho statistic is used to estimate a rank-based measure of association. These are more robust and have been recommended if the data do not come from a bivariate normal distribution.
 
2.p-value of correlation coefficient (Significance levels)
The function cor.test() can be used to compute the significance level for correlation. It tests for association between paired samples using pearson, kendall or spearman methods.
 
The simplified format is :
 
# x and y are numeric vectors with the same length
cor.test(x, y, method=c("pearson", "kendall", "spearman"))
The value returned by the function is a list containing, among others, the following components :
 
statistic the value of the test statistic.
p.value the p-value of the correlation test.
estimate correlation coefficient : “cor” (for pearson), “tau” (for kendall) or “rho” (for spearman)
 
3. Pearson correlation test
The test statistic follows a t distribution with length(x)-2 degrees of freedom if the samples follow independent normal distributions.
 
res<-cor.test(x,y, method="pearson")
res
 
Pearson's product-moment correlation
data: x and y
t = 1.841, df = 7, p-value = 0.1082
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.1497 0.8956
sample estimates:
cor
0.5712
cor is the correlation coefficient.
 
The correlation coefficient between x and y are 0.5712 and the p-value is 0.1082.
 
5. Kendall rank correlation test
The Kendall rank correlation coefficient or Kendall’s tau statistic is used to estimate a rank-based measure of association. This test may be used if the data do not necessarily come from a bivariate normal distribution.
 
res<-cor.test(x,y, method="kendall")
res
 
Kendall's rank correlation tau
data: x and y
T = 26, p-value = 0.1194
alternative hypothesis: true tau is not equal to 0
sample estimates:
tau
0.4444
tau is the Kendall correlation coefficient.
 
The correlation coefficient between x and y are 0.4444 and the p-value is 0.1194.
 
6. Spearman’ rank correlation coefficient
Spearman’s rho statistic is also used to estimate a rank-based measure of association. This test may be used if the data do not come from a bivariate normal distribution.
 
res<-cor.test(x,y, method="spearman")
res
 
Spearman's rank correlation rho
data: x and y
S = 48, p-value = 0.0968
alternative hypothesis: true rho is not equal to 0
sample estimates:
rho
0.6
rho is the Spearman’s correlation coefficient.
 
The correlation coefficient between x and y are 0.6 and the p-value is 0.0968.