R 基于列值从列表中选择数据帧

R 基于列值从列表中选择数据帧,r,list,R,List,使用R,我试图创建一个新的dataframes列表,仅当其中一列中存在特定的值组合时,才从现有列表中选择dataframes。让我来解释一下第一步的工作原理。这是我在称为df的数据帧中的原始数据: Taxon C N func.group trophic.grp 1 Chrysomelidae.Phylotreta.exclamationis -30.23 5.06 grazer her

使用R,我试图创建一个新的dataframes列表,仅当其中一列中存在特定的值组合时,才从现有列表中选择dataframes。让我来解释一下第一步的工作原理。这是我在称为df的数据帧中的原始数据:

                                  Taxon      C     N    func.group  trophic.grp
1  Chrysomelidae.Phylotreta.exclamationis -30.23  5.06     grazer   herbivore
2        Chrysomelidae.Neocrepidodera.sp. -27.29  5.55     grazer   herbivore
3        Chrysomelidae.Neocrepidodera.sp. -27.84  5.54     grazer   herbivore
4        Chrysomelidae.Neocrepidodera.sp. -27.69  4.59     grazer   herbivore
5              Mitidulidae.Meligethes.sp. -26.99  5.30     grazer   herbivore
6           Chrysomelidae.Phylotreta.sp.2 -28.50  2.40     grazer   herbivore
7           Chrysomelidae.Phylotreta.sp.2 -28.36  4.17     grazer   herbivore
8           Chrysomelidae.Phylotreta.sp.2 -29.50  3.15     grazer   herbivore
9           Chrysomelidae.Phylotreta.sp.2 -27.69  3.72     grazer   herbivore
10          Chrysomelidae.Phylotreta.sp.2 -28.22  3.26     grazer   herbivore
11                  Gastropoda.snail.sp.1 -26.21  3.54     grazer   herbivore
12                  Gastropoda.snail.sp.1 -27.59  2.61     grazer   herbivore
13                  Gastropoda.snail.sp.1 -25.10  2.66     grazer   herbivore
14                  Gastropoda.snail.sp.2 -26.49  2.55     grazer   herbivore
15                  Gastropoda.snail.sp.4 -27.46 -0.38     grazer   herbivore
16       Lepidoptera.Arctidae.Ermine.moth -28.51  2.44     grazer   herbivore
17       Curculionidae.Ischapterapion.sp. -29.06  2.19     weevil   herbivore
18       Curculionidae.Ischapterapion.sp. -29.27  1.60     weevil   herbivore
19       Curculionidae.Ischapterapion.sp. -29.94  2.08     weevil   herbivore
20       Curculionidae.Ischapterapion.sp. -29.71  2.16     weevil   herbivore
21            Curculionidae.Protapion.sp. -28.45  1.91     weevil   herbivore
22            Curculionidae.Protapion.sp. -25.99  0.55     weevil   herbivore
23            Curculionidae.Protapion.sp. -28.27  1.52     weevil   herbivore
24            Curculionidae.Protapion.sp. -28.01  1.74     weevil   herbivore
25            Curculionidae.Protapion.sp. -27.06  0.54     weevil   herbivore
26             Curculionidae.Hypera.meles -25.41  3.38     weevil   herbivore
27               Curculionidae.Sitona.sp. -27.05  2.01     weevil   herbivore
28               Curculionidae.Sitona.sp. -26.70  3.07     weevil   herbivore
29               Curculionidae.Sitona.sp. -27.64  2.13     weevil   herbivore
30               Curculionidae.Sitona.sp. -27.50  1.47     weevil   herbivore
31            Curculionidae.Phylobius.sp. -28.27  2.66     weevil   herbivore
32      Curculionidae.Hypera.nigrorostris -25.52  2.43     weevil   herbivore
这个数据帧(df)包含14个不同的“分类单元”,其中一些具有多个样本,因此总共有32个样本。每个分类单元也按“功能群”列分类为“食草动物”或“象鼻虫”

首先,我想从我的14个分类单元中随机选择6个分类单元,用于6个分类单元的所有可能组合。因此,有大约3003个6个分类单元的组合,可以从14个分类单元中选出(随机抽样,无需替换,顺序不重要)。对于选择的每个分类单元,我想包括该分类单元的所有样本。我使用此代码,该代码运行良好:

combos<-combn(unique(as.character(df$Taxon)), 6) 
mysamples <- apply(combos, 2, function(vec) df[ df$Taxon %in% vec, ] )
combos试试这个代码

mysamples[unlist(lapply(mysamples,
                        function(x) !any(is.na(match(levels(df$func.group),
                                                     x$func.group)))))]
如果缺少食草动物或象鼻虫,
match
返回NA,因此
any
将返回TRUE,该值为倒数(!),因此此数据帧不会在最后一个数据帧中使用

尝试此操作

df.list <- lapply(mysamples,
                  function(x){if(any(x$func.group=="grazer")&
                                 any(x$func.group=="weevil"))
                                 return(x)})

both <- Filter(Negate(is.null),df.list)
df.list