简体   繁体   中英

In R, do correlation between a column of a data frame between all columns in another data frame?

I am trying to determine the correlation between meta genes from running NMF with clinical metadata. As such, I have two data frames to work with. The first being the h matrix with Metagene expression for each patient. The other data frame being the metadata. I would want to run separate correlations between n Metagenes with the metadata data frame.

The matrix with n meta genes

   Metagene 1     Metagene 2    Metagene N
P1   0.434          0.454
P2   0.322          0.343
P3   0.343          0.323

I want to run a correlation of a column of this above matrix with the metadata matrix (about 30+ columns).

      Age    BMI    etc
P1    43.4   45.4
P2    32.2   34.3
P3    34.3   32.3

From my own attempts and from my research, I could only correlate two whole data frames and not one specific column with another whole data frame. Any advice would be appreciated by this newbie thank you!

I would simply cbind the two matrices, run a cor on the resulting matrix und subset the relevant columns and rows:

You did not provide a reprex for your data, hence I came up with a sample set illustrating the idea:

## Sample Data
set.seed(123)
mg <- matrix(rnorm(1000), ncol = 10)
colnames(mg) <- paste0("Metagene_", 1:10)
md <- matrix(rnorm(400), ncol = 4)
colnames(md) <- c("Age", "BMI", "Weight", "Height")

## Calculate all correlations
all_cors <- cor(cbind(mg, md))
## Extract the relevant rows and columns
all_cors[which(rownames(all_cors) %in% colnames(md)), 
         which(colnames(all_cors) %in% colnames(mg)), 
         drop = FALSE]

#         Metagene_1    Metagene_2  Metagene_3  Metagene_4 Metagene_5  Metagene_6
# Age     0.07578378 -0.0237288723 -0.16055711  0.19574314 0.03423165 -0.04127698
# BMI    -0.12758456  0.0126769273  0.08534472  0.01303423 0.14617045  0.03426132
# Weight -0.04952921  0.0008581215  0.13484638 -0.01789764 0.07973140  0.09242346
# Height  0.13040172 -0.0973757690 -0.02717946  0.09158976 0.10353301 -0.02002915
#         Metagene_7  Metagene_8  Metagene_9 Metagene_10
# Age     0.08490306  0.14588393 -0.08666105   0.1722885
# BMI     0.17938487 -0.04331769 -0.01524405  -0.1039355
# Weight  0.02342581  0.16759941  0.16386263  -0.0433296
# Height  0.05756989  0.04728934 -0.06805982   0.1269274

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM