首页 > 解决方案 > 基于数据框对大矩阵进行子集化

问题描述

我正在使用 R 中的一些基因组文件。我有一个大矩阵,其格式如下例所示,其中列是样本,行是基因(实际矩阵有 205 列和超过 22k 行。

          GSM1304852  GSM1304853 GSM1304854 GSM1304855
1007_s_at  2.3945368  2.27518369  2.1611630  1.9641833
1053_at    0.1051084  0.06160802  0.3421762  0.3593916
117_at    -0.4597124 -0.52310349 -0.4436059 -0.6370277
121_at     0.9333566  1.13180904  0.9975700  1.0079778

我还有一个数据框,其格式类似于下面的示例,其中geo_accession可以在矩阵的第一行中找到相同的 id。

                    title geo_accession Age    Disease_State Gender  pH  PMI Race RIN      tissue
GSM1304852 bipolar_hip_10    GSM1304852  52 bipolar disorder      M 6.7 23.5    W 6.3 hippocampus
GSM1304853 bipolar_hip_11    GSM1304853  50 bipolar disorder      F 6.4 11.7    W 6.8 hippocampus
GSM1304854 bipolar_hip_12    GSM1304854  28 bipolar disorder      F 6.3 22.3    W 7.7 hippocampus
GSM1304855 bipolar_hip_13    GSM1304855  55 bipolar disorder      F 6.4 17.5    W 7.6 hippocampus
GSM1304856 bipolar_hip_14    GSM1304856  58 bipolar disorder      M 6.8 27.7    W 7.0 hippocampus
GSM1304857 bipolar_hip_15    GSM1304857  28 bipolar disorder      M 6.2 27.4    W 7.7 hippocampus

我需要对与某个组织相关的矩阵中的所有列进行子集化(在完整的数据框中有 3 种组织),因此最后,我需要有 3 个矩阵。

例如:从矩阵中我只想获取与相关联的列hippocampus

matrix # an R matrix object
DataFrame # an R dataframe

DFhip <- DataFrame[ which(tissue == 'hippocampus',]
GSMlist <- DFhip$geo_accesion

MatrixHip <- matrix[GSMlist,] # I know this is the wrong syntax, it's just to let you understand 

我对 R 比较陌生,而且我不习惯子集矩阵。

标签: r

解决方案


由于第一个矩阵中的 colnames 与 geo_accession 相同,因此您可以直接使用后者来对列进行子集化:

hippocamups <- your_large_matrix[, your_dataframe$geo_accession[your_dataframe$tissue == "Hippocapmus"] ]

推荐阅读