[英]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. 我有大约4000行和220列的数据集。 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.
出于研究和分析的原因,我创建了一个相关矩阵,并滤出了所有大于0.4的值,以便进一步研究这些功能。
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. 我已经将其转换为数据框,但是左侧只有行标签,我想添加/绑定另一列,该列将向我显示与col值相对应的列名。
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. 无需使用
tidyr
, base
merge
可以完美地完成此任务。
Using mtcars
I get this: 使用
mtcars
我得到这个:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.