用R中的另一个数据集过滤数据集 [英] Filtering a Dataset by another Dataset in R

查看:70
本文介绍了用R中的另一个数据集过滤数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要完成的任务实质上是通过 id列中的条目根据另一个数据集中的条目过滤一个数据集。我正在使用的数据集非常大,有十万个条目和三十个左右的变量。我已经制作了玩具数据集来帮助解释我想做什么。

The task I am trying to accomplish is essentially filtering one dataset by the entries in another dataset by entries in an "id" column. The data sets I am working with are quite large having 10 of thousands of entries and 30 or so variables. I have made toy datasets to help explain what I want to do.

第一个数据集包含一个条目列表,每个条目都有自己的唯一登录号(这是

The first dataset contains a list of entries and each entry has their own unique accession number(this is the id).

Data1 = data.frame(accession_number = c('a','b','c','d','e','f'), values =c('1','3','4','2','3','12'))
>Data1
      accession_number values
1                a      1
2                b      3
3                c      4
4                d      2
5                e      3
6                f     12

我只对登录号为'c','d'和'e'的条目感兴趣。 (实际上,尽管我的名单大约有100个唯一的加入号)。接下来,我创建了一个仅具有唯一登录号而没有其他值的数据框。

I am only interested in the entries that have the accession number 'c', 'd', and 'e'. (In reality though my list is around 100 unique accession numbers). Next, I created a dataframe with the only the unique accession numbers and no other values.

>SubsetData1
  accession_number
1                c
2                d
3                e

第二个数据我希望过滤的集合包含多个条目,其中一些条目的登录号相同。

The second data set, which i am looking to filter, contains multiple entries some which have the same accession number.

>Data2
   accession_number values  Intensity col4       col6
1                 a      1 -0.0251304    a -0.4816370
2                 a      2 -0.4308735    b -1.0335971
3                 c      3 -1.9001321    c  0.6416735
4                 c      4  0.1163934    d -0.4489048
5                 c      5  0.7586820    e  0.5408650
6                 b      6  0.4294415    f  0.6828412
7                 b      7 -0.8045201    g  0.6677730
8                 b      8 -0.9898947    h  0.3948412
9                 c      9 -0.6004642    i -0.3323932
10                c     10  1.1367578    j  0.9151915
11                c     11  0.7084980    k -0.3424039
12                c     12 -0.9618102    l  0.2386307
13                c     13  0.2693441    m -1.3861064
14                d     14  1.6059971    n  1.3801924
15                e     15  2.4166472    o -1.1806929
16                e     16 -0.7834619    p  0.1880451
17                e     17  1.3856535    q -0.7826357
18                f     18 -0.6660976    r  0.6159731
19                f     19  0.2089186    s -0.8222399
20                f     20 -1.5809582    t  1.5567113
21                f     21  0.3610700    u  0.3264431
22                f     22  1.2923324    v  0.9636267

我要做的是比较第一个数据集(SubsetData1)的子集列表和第二个数据集(Data2),以创建过滤后的仅包含以下内容的数据集具有在子列表中定义的相同登录号。过滤后的数据集应如下所示。

What im looking to do is compare the subsetted list of the first data set(SubsetData1), with the second dataset (Data2) to create a filtered dataset that only contains the entries that have the same accession numbers defined in the subsetted list. The filtered dataset should look something like this.

   accession_number values  Intensity col4       col6
9                 c      9 -0.6004642    i -0.3323932
10                c     10  1.1367578    j  0.9151915
11                c     11  0.7084980    k -0.3424039
12                c     12 -0.9618102    l  0.2386307
13                c     13  0.2693441    m -1.3861064
14                d     14  1.6059971    n  1.3801924
15                e     15  2.4166472    o -1.1806929
16                e     16 -0.7834619    p  0.1880451
17                e     17  1.3856535    q -0.7826357

我不知道是否需要开始执行循环才能解决此问题,或者是否有简单的R命令可以帮助我完成此任务。

I don't know if I need to start making loops in order to tackle this problem, or if there is a simple R command that would help me accomplish this task. Any help is much appreciated.

谢谢

推荐答案

尝试一下

WantedData=Data2[Data2$ccession_number %in% SubsetData1$accession_number, ]

这篇关于用R中的另一个数据集过滤数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆