保留与矩阵R相同列名的数据框行

问题描述 投票:0回答:1

我想保留只有13列的矩阵的数据框的行(20行),这意味着消除与我的矩阵的列名同名的数据框的行。因此最终以13行的最终数据帧结束,这是因为我想在将来绘制热图时将此数据帧注释到我的矩阵中。

我不确定这是否真的可能:S

矩阵:enter image description here

DataFrame:

enter image description here

r dataframe matrix devtools
1个回答
0
投票

样本数据:

Mydataframe <- data.frame(casecontrol = c(rep("case",10),rep("Control",10)),
                          condition=c(rep("cond1",5),rep("cond2",5),rep("cond3",5),rep("cond4",5)))
row.names(Mydataframe) <- sapply(1:20, function(x) paste0("sample",x))

Mymatrix <- matrix(0,nrow=10,ncol=13)
colnames(Mymatrix) <- sapply(1:13, function(x) paste0("sample",x))

您可以找到匹配项,并按照以下说明将其删除:

RowsToRemove <- match(colnames(Mymatrix),row.names(Mydataframe))
MyNewdataframe <- Mydataframe[-RowsToRemove,]

输出

> MyNewdataframe
         casecontrol condition
sample14     Control     cond3
sample15     Control     cond3
sample16     Control     cond4
sample17     Control     cond4
sample18     Control     cond4
sample19     Control     cond4
sample20     Control     cond4
© www.soinside.com 2019 - 2024. All rights reserved.