在 R 中的对称矩阵中按行和列进行选择 [英] Selecting by both rows and columns in a symmetrical matrix in R

查看:68
本文介绍了在 R 中的对称矩阵中按行和列进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对称的数据框,想选择数据的一个子集用于分析.这意味着选择所需的行和列并保持正确的顺序,这样新的数据框仍然是一个对称的立方体.使用示例数据:

I have a symmetrical dataframe and would like to select a subset of the data to use for analysis. This means selecting both the desired rows and columns and maintaining the right order so the new dataframe is still a symmetrical cube. With example data:

# Example data 
Sample <- c('Sample_A', 'Sample_B', 'Sample_C', 'Sample_D', 'Sample_E') 
Sample_A <- c(0, 3.16, 1, 1.41, 3) 
Sample_B <- c(3.16, 0, 3, 2.83, 1) 
Sample_C <- c(1, 3, 0, 1, 2.83) 
Sample_D <- c(1.41, 2.83, 1, 0, 2.65) 
Sample_E <- c(3, 1, 2.83, 2.65, 0) 
df = data.frame(Sample, Sample_A, Sample_B, Sample_C, Sample_D, Sample_E)
df

然后我单独定义我感兴趣的样本,例如

Then I separately define the samples I'm interested in e.g.

samples_to_use <- c("Sample_B", "Sample_D", "Sample_E")

我想要的结果是这样的

# Desired output
Sample <- c('Sample_B', 'Sample_D', 'Sample_E')
Sample_B <- c(0, 2.83, 1)
Sample_D <- c(2.83, 0, 2.65)
Sample_E <- c(1, 2.65, 0)
df_2 = data.frame(Sample, Sample_B, Sample_D, Sample_E)
df_2

即我选择与 samples_to_use 匹配的行和列.

i.e. I select the rows and columns that match samples_to_use.

我已经尝试通过将 df 与 samples_to_use 的数据帧合并来单独选择行,但这似乎不雅,并且还给我留下了与行不再匹配的错误列.寻找更优雅的解决方案,谢谢!

I've tried separately selecting the rows by merging df with a dataframe of samples_to_use but that seems inelegant and also leaves me with the wrong columns that no longer match the rows. Looking for a more elegant solution, thanks!

推荐答案

我们可以使用列索引和 'samples_to_use' 而行索引可以是一个逻辑索引来检查 'samples_to_use' 元素是否为 %in% 'Sample' 列

We can use column index with 'samples_to_use' while the row index can be a logical index to check whether the 'samples_to_use' elements are %in% the column 'Sample'

df[df$Sample %in% samples_to_use, c("Sample", samples_to_use)]

注意:Is 不是对称矩阵.如果需要是对称矩阵,第一列应该被删除,它应该是行名并将'data.frame'转换为'matrix'

NOTE: Is is not a symmetric matrix. If it needs to be a symmetric matrix, the first column should be removed and it should be row names and convert the 'data.frame' to 'matrix'

m1 <- as.matrix(df[-1])
row.names(m1) <- df$Sample

然后,子集更容易

m1[samples_to_use, samples_to_use]
#         Sample_B Sample_D Sample_E
#Sample_B     0.00     2.83     1.00
#Sample_D     2.83     0.00     2.65
#Sample_E     1.00     2.65     0.00

这篇关于在 R 中的对称矩阵中按行和列进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆