R 基于数据帧的大型矩阵子集
我正在处理R中的一些基因组文件。我有一个大矩阵,格式如下面的示例,其中列是样本,行是基因(真实矩阵有205列和超过22k行)R 基于数据帧的大型矩阵子集,r,R,我正在处理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 -
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_accessition
与矩阵第一行中的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个矩阵
例如:从矩阵中,我只想取与海马体相关的列:
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
matrix#一个R矩阵对象
数据帧#R数据帧
DFhip我不确定是否理解了您的问题,但如果您想为ine行子集数据帧,请使用:
data1<-data[data[1] %in% c("GSM1304852","GSM1304855")]
data1由于第一个矩阵中的colname与geo_accession相同,因此可以直接使用后者来对列进行子集划分:
hippocamups <- your_large_matrix[, your_dataframe$geo_accession[your_dataframe$tissue == "Hippocapmus"] ]
hippocamups如果矩阵定义了colname,请尝试以下操作:
lapply(function(x) {
matrix[, unique(df$geo_accession[df$tissue == x])]
}, x = unique(df$tissue))
矩阵是你的矩阵,df是你的数据帧
希望有帮助谢谢你的回答,他们帮了我很大的忙。他的两行代码让一切顺利:
hippocampus <- filter(GSMdata, tissue == 'hippocampus')$geo_accession
hippocampus <- data[, hippocamups]
海马体Ok,例如:我想找到所有来自海马体的geo_加入
,因此我过滤我的数据帧,只取geo_加入
列。现在我想从矩阵中获取与过滤的实例匹配的所有列