- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the correlation between corresponding columns of two matrices in R?
To find the correlation between corresponding columns of two matrices, we can use mapply function but we will have to read the matrices using as.data.frame function. For example, if we have two matrices called M_1 and M_2 and each of these matrices contains 5 columns then the correlation between corresponding columns of these matrices can be found by using the command mapply(cor,as.data.frame(M_1),as.data.frame(M_2))
Example
Consider the below matrices −
M1<-matrix(1:40,ncol=2) M1
Output
[,1] [,2] [1,] 1 21 [2,] 2 22 [3,] 3 23 [4,] 4 24 [5,] 5 25 [6,] 6 26 [7,] 7 27 [8,] 8 28 [9,] 9 29 [10,] 10 30 [11,] 11 31 [12,] 12 32 [13,] 13 33 [14,] 14 34 [15,] 15 35 [16,] 16 36 [17,] 17 37 [18,] 18 38 [19,] 19 39 [20,] 20 40
Example
M2<-matrix(1:40,ncol=2) M2
Output
[,1] [,2] [1,] 1 21 [2,] 2 22 [3,] 3 23 [4,] 4 24 [5,] 5 25 [6,] 6 26 [7,] 7 27 [8,] 8 28 [9,] 9 29 [10,] 10 30 [11,] 11 31 [12,] 12 32 [13,] 13 33 [14,] 14 34 [15,] 15 35 [16,] 16 36 [17,] 17 37 [18,] 18 38 [19,] 19 39 [20,] 20 40
Finding the correlation between column 1 of M1 and column 1 of M2 for both the columns −
Example
mapply(cor,as.data.frame(M1),as.data.frame(M2))
Output
V1 V2 1 1
Example
M3<-matrix(rpois(40,5),ncol=2) M3
Output
[,1] [,2] [1,] 3 6 [2,] 4 6 [3,] 9 3 [4,] 1 7 [5,] 6 5 [6,] 4 2 [7,] 1 3 [8,] 1 3 [9,] 5 2 [10,] 4 1 [11,] 3 5 [12,] 6 8 [13,] 3 4 [14,] 6 6 [15,] 3 5 [16,] 3 6 [17,] 4 5 [18,] 4 5 [19,] 5 4 [20,] 3 7
Example
M4<-matrix(rpois(40,5),ncol=2) M4
Output
[,1] [,2] [1,] 3 4 [2,] 7 5 [3,] 6 5 [4,] 8 4 [5,] 7 5 [6,] 6 4 [7,] 8 0 [8,] 3 5 [9,] 2 7 [10,] 6 6 [11,] 4 4 [12,] 6 5 [13,] 8 4 [14,] 5 8 [15,] 4 2 [16,] 7 2 [17,] 6 4 [18,] 3 2 [19,] 4 5 [20,] 4 7
Finding the correlation between column 1 of M3 and column 1 of M4 for both the columns −
Example
mapply(cor,as.data.frame(M3),as.data.frame(M4))
Output
V1 V2 -0.06220599 0.01182284
- Related Articles
- How to multiply corresponding values from two matrices in R?
- How to get the correlation between two columns in Pandas?
- How to find the mean of corresponding elements of multiple matrices in R?
- How to find the correlation coefficient between two data frames in R?
- How to find the correlation coefficient between rows of two data frames in R?
- How to find the dot product of two matrices in R?
- Correlation between two numeric columns in a Pandas DataFrame
- How to combine matrices having same number of columns in R?
- How to find the correlation matrix by considering only numerical columns in an R data frame?
- How to find the correlation for data frame having numeric and non-numeric columns in R?
- Find the common elements between two columns of an R dataframe.
- Find the combination of columns for correlation coefficient greater than a certain value in R
- How to find the sequence of correlation between variables in an R data frame or matrix?
- How to multiply two matrices by elements in R?
- How to find the difference in number of days between two date columns of an R data frame?

Advertisements