查找两个数据框中的公共列并仅保留两个数据框中公共的列

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

我有一个包含基因名称的数据集。在该数据集中,我想从另外两个数据集中提取它们,即

gd
cd

common_genes
是一个向量,其中包含我想要搜索的基因名称

我需要有关如何使用共同基因在

cd
gd
数据集中拥有共同列的帮助。这是因为我的分析需要我对这两个数据集进行比较。

#Extract those that are present in the `gd` dataset.
common_genes <- intersect(gene_names, colnames(gd))
# extract these 300 genes too from the `gd` for common genes
A <- gd[, common_genes]
#Extract these 300 genes too from the `cd` dataset.
common_genes2 <- intersect(gene_names, colnames(cd))
B<-cd[,common_genes]

我得到的输出是 300 个基因中的

A
150 个基因和 300 个基因中的
B
200 个基因。

我想要的输出是下面的示例:

A
RPL26 MS4A1 ELK1 SNIP1
200   300     400     534
B
RPL26 MS4A1 ELK1 SNIP1
100    81    91  112
r dataframe dplyr data-cleaning
1个回答
0
投票

由于您的示例不可重现,所以我创建了自己的示例

df1 <- data.frame(
    a = 1:3,
    b = 2:4,
    c = 3:5)
df2 <- data.frame(
    b = 4:6,
    c = 5:7,
    d = 6:8)

# it's unclear to me why you wouldn't think to use intersect, when it's right there in your question?
common_cols <- intersect(colnames(df1), colnames(df2))

df1 <- df1[,common_cols]
df2 <- df2[,common_cols]

df1 之后:

  b c
1 2 3
2 3 4
3 4 5
© www.soinside.com 2019 - 2024. All rights reserved.