基于另一个数据框/列表对数据框中的列进行子集化 [英] subset a column in data frame based on another data frame/list
问题描述
我有以下 table1
这是一个由 6 列和 8083 行组成的数据框.下面我显示这个 table1
的头部:
I have the following table1
which is a data frame composed of 6 columns and 8083 rows. Below I am displaying the head of this table1
:
|gene ID | prom_65| prom_66| amast_69| amast_70| p_value|
|:--------------|---------:|---------:|---------:|---------:|---------:|
|LdBPK_321470.1 | 24.7361| 25.2550| 31.2974| 45.4209| 0.2997430|
|LdBPK_251900.1 | 107.3580| 112.9870| 77.4182| 86.3211| 0.0367792|
|LdBPK_331430.1 | 72.0639| 86.1486| 68.5747| 77.8383| 0.2469355|
|LdBPK_100640.1 | 43.8766| 53.4004| 34.0255| 38.4038| 0.1299948|
|LdBPK_330360.1 | 2382.8700| 1871.9300| 2013.4200| 2482.0600| 0.8466225|
|LdBPK_090870.1 | 49.6488| 53.7134| 59.1175| 66.0931| 0.0843242|
我有另一个数据框,名为 accessions40
,它是一个包含 510 个基因 ID 的列表.它是 table1
第一列的子集,即它的所有值 (510) 都包含在 table1
(8083) 的第一列中.accessions40
的头部显示如下:
I have another data frame, called accessions40
which is a list of 510 gene IDs. It is a subset of the first column of table1
i.e. all of its values (510) are contained in the first column of table1
(8083). The head of accessions40
is displayed below:
|V1 |
|:--------------|
|LdBPK_330360.1 |
|LdBPK_283000.1 |
|LdBPK_360210.1 |
|LdBPK_261550.1 |
|LdBPK_367320.1 |
|LdBPK_361420.1 |
我想要做的是:我想生成一个新的 table2
,它在第一列(基因 ID)下只包含 accessions40
和table1
中其他五列的相应值.换句话说,我想根据 accessions40
的值对 table1
的第一列进行子集化.
What I want to do is the following: I want to produce a new table2
which contains under the first column (gene ID) only the values present in accessions40
and the corresponding values from the other five columns from table1
. In other words, I want to subset the first column of my table1
based on the values of accessions40
.
推荐答案
我们可以使用 %in%
得到一个逻辑向量和 subset
'table1 的行'基于此.
We can use %in%
to get a logical vector and subset
the rows of the 'table1' based on that.
subset(table1, gene_ID %in% accessions40$V1)
<小时>
更好的选择是 data.table
library(data.table)
setDT(table1)[gene_ID %chin% accessions40$V1]
或者使用 dplyr
library(dplyr)
table1 %>%
filter(gene_ID %in% accessions40$V1)
这篇关于基于另一个数据框/列表对数据框中的列进行子集化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!