简体   繁体   中英

How to create a row name, row index, col index, col name from correlation matrix converted to df in R?

I have a data set of around 4000 rows and 220 columns. For research and analysis reasons I have created a correlation matrix and filtered out all the values that are greater then 0.4 for further research of the features.

Here is what I did:

df_high_corr <- which((res > 0.4 & res < 1), arr.ind = T)
res1 <- as.data.frame(df_high_corr))

I have converted it to data frame but I have only row labels to the left, I want to add/bind another column that will display me the column names corresponding to the col value.

For example:

__________|_row__|_col____|_col_name______
DM.RESY   | 18   |  6     | dummy_col_name
DM.MARIT  | 19   |  6     | dummy_col_name
PHX.dage  |198   |  6     | dummy_col_name
CRS.VSCLR |206   |  6     | dummy_col_name
QH.HENGY  | 61   | 12     | dummy_col_name2
QC.CVWSF  | 41   | 13     | dummy_col_name3

Please suggest me a way to do it - fast creative and easy, I want to learn the best practices to do it.

Try this:

foo <- data.frame(col_name = colnames(res), col = 1:ncol(res))
merge(which((res > 0.4 & res < 1), arr.ind = T), foo)

There's no need to use tidyr , base merge does this perfectly.

Using mtcars I get this:

res <- cor(mtcars)
foo <- data.frame(col_name = colnames(res), col = 1:ncol(res))
bar <- merge(which((res > 0.4 & res < 1), arr.ind = T), foo)
head(bar)

  col row col_name
1   1   5      mpg
2   1   7      mpg
3   1   8      mpg
4   1   9      mpg
5   1  10      mpg
6   2   3      cyl

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