简体   繁体   中英

Calculating non-pairwise correlation between different sets of columns of a dataset

Suppose to calculate correlation of dataset mtcars we can use corr <- round(cor(mtcars), 1) , or calculate and plot a correlation matrix using corrplot(cor(mtcars) . However, the product is pairwise correlation. Is it possible to calculate non-pairwise correlation between spesific set of columns ie, in mtcars correlation between columns 1:3 ( mpg, cyl, disp ) VS column 4:8 (hp, drat, wt, qsec, vs) .

Please suggest any method.

I have a data set containing 120 columns. I want to calculate/plot correlation matrix (Spearman correlate) of columns 1:50 with rest of the columns (ie, 51-120). Please tell me the solution, if possible, by using cor , or packages ie, corrplot , ggcorrplot .

Thanks in anticipation.

You can do this using the cor function, just need to specify the second parameter. Example:

vertical_axis <- 1:3
horizontal_axis <- 4:8
corr <- cor(mtcars[, vertical_axis], mtcars[, horizontal_axis])
corrplot(corr)

The result:

相关图演示


NOTE: Using the corrplot function with the order parameter will produce errors or a wrong plot. If you need to reorder the variables (ex.: with hclust ), reorder the correlation matrix before passing it to corrplot .

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