Correlation in R can be calculated using cor() function. In R, Cor() function is used to calculate correlation among vectors, Matrices and data frames.
Syntax for correlation function in R:
x | A vector, matrix or data frame |
y | A vector, matrix or data frame with compatible dimensions to x |
method | a character string indicating which correlation coefficient is to be computed (i) “pearson” (default), (ii)”kendall”, or(iii) “spearman”. |
Correlation of vector in R:
# correlation of vectors in R x <- c(0,1,1,2,3,5,8,13,21,34) y <- log(x+1) cor(x,y)
the above code calculates correlation coefficient of vectors x and y which results in the output
Which says that these two vectors are highly positively correlated
Correlation of vector in R with NA:
Note: Correlation in R cannot be calculated if values has NA. So use = “complete.obs” neglects NAs while calculating correlation coefficient in R
# correlation in R : handling NA x <- c(0,1,1,2,3,5,8,13,21,NA) y <- log(x+1) cor(x,y,use = "complete.obs")
so the output will be
Correlation matrix of data frame in R:
Lets use mtcars data frame to demonstrate example of correlation matrix in R. lets create a correlation matrix of mpg,cyl,display and hp against gear and carb.
# correlation matrix in R using mtcars dataframe x <- mtcars[1:4] y <- mtcars[10:11] cor(x, y)
so the output will be a correlation matrix
gear carb
mpg 0.4802848 -0.5509251
cyl -0.4926866 0.5269883
disp -0.5555692 0.3949769
hp -0.1257043 0.7498125
Correlation of Matrix in R:
In the below example we have calculated correlation of matrices.
# correlation of Matrix in R matA<-matrix(1:9,3,3) matB<-matrix(c(10,11,12,15,16,20,21,26,28),3,3) cor(matA,matB)
output will be a correlation matrix as shown below
[,1] [,2] [,3]
[1,] 1 0.9449112 0.9707253
[2,] 1 0.9449112 0.9707253
[3,] 1 0.9449112 0.9707253