【问题标题】:Subsetting by data in two columns按两列数据进行子集化
【发布时间】:2019-07-14 09:25:25
【问题描述】:

我有一个如下结构的数据集

     site    block treatment date insect1 insect2 insect3 insect4 ...
1  location1     a  chemical1 date1  0     0      10       1          
2  location1     a  chemical2 date1  1     0       2       0   
3  location1     a  chemical3 date1  0     0      23       1   
4  location1     a  chemical4 date1  0     0       5       0   
5  location1     a  chemical5 date1  0     0       9       0   
6  location1     b  chemical1 date1  0     1       5       0   
7  location1     b  chemical2 date1  1     0       5       1   
8  location1     b  chemical3 date1  0     0       4       0   
9  location1     b  chemical4 date1  0     0       5       0   
10 location1     b  chemical5 date1  3     0      12       0   
11 location1     c  chemical1 date1  0     0       2       1   
12 location1     c  chemical2 date1  0     0       0       0   
13 location1     c  chemical3 date1  0     0       4       0   
14 location1     c  chemical4 date1  0     0       2       7   
15 location1     c  chemical5 date1  2     0       5       0   
16 location1     d  chemical1 date1  0     0       8       1   
17 location1     d  chemical2 date1  0     0       3       0   
18 location1     d  chemical3 date1  0     0      10       0   
19 location1     d  chemical4 date1  0     0       2       0   
20 location1     d  chemical5 date1  0     1       7       0
       .         .     .        .    .     .       .       .   
       .         .     .        .    .     .       .       .   
       .         .     .        .    .     .       .       .

此数据集是我进行的一项实验的结果,该实验测试了不同五种不同的化学处理(化学品 1-5)对田间场地(此处为昆虫 1-4)的吸引力的影响(位置 1)。该实验在该现场的不同位置被阻止(a-d)4 次,并在不同日期重复 5 次(仅显示日期 1)。所有这些信息都存储在我的数据集的前四列中。

接下来的一系列列(我有 46 个,但我只显示 4 个)表示不同种类的昆虫,以及我用特定化学物质捕获的昆虫数量在每个处理 x 块 x 日期组合内(=每一行)。

作为我分析的一部分,我想遍历这个数据集并找到我没有捕获昆虫的每只昆虫的块 x 日期的组合。例如,我在日期 1 的块 a 或 c 中没有捕获昆虫 2 的个体,因此我想将其从我的最终数据集中删除以进行分析。

我花了很多时间编写代码来完成这项任务,但昨晚我发现我的代码并没有像我想象的那样工作,我正在努力解决这个问题。这是到目前为止的代码(我已经包含了解决问题的所有步骤,因此人们可以看到问题可能出现在哪里,或者提出更好的处理方式......):

创建一个列表,使每种昆虫(此处为第 5-8 列)都有自己的数据框

sticky.list = lapply(sticky[-c(1:4,50)], function(i)data.frame(site=sticky$site, 
                                                          block=sticky$block,
                                                          treatment=sticky$treatment,
                                                          date=sticky$date,
                                                          number=as.numeric(i)))

作为我的列表的一部分创建的数据框之一的示例

$insect1
       site    block     treatment date     number
1  location1     a       chemical1 date1      0
2  location1     a       chemical2 date1      1
3  location1     a       chemical3 date1      0
4  location1     a       chemical4 date1      0
5  location1     a       chemical5 date1      0

然后在列表中具有数据框名称(即昆虫名称)的每个数据框中添加一个新列

temp.list = Map(cbind, sticky.list, morphotype = names(sticky.list))  

       site    block   treatment date     number morphotype
1  location1     a     chemical1 date1      0      insect1
2  location1     a     chemical2 date1      1      insect1      
3  location1     a     chemical3 date1      0      insect1
4  location1     a     chemical4 date1      0      insect1
5  location1     a     chemical5 date1      0      insect1

通过垂直组合制作更大的数据集,然后展平每个列表元素(即制作一个大数据框。这会将我之前列表中的所有数据框放在一个数据框中。

sticky.list.combined.df <- temp.list %>% bind_rows(temp.list) %>% # make larger sample data
  mutate_if(is.list, simplify_all) %>% # flatten each list element internally 
  unnest()

按块和形态类型分组,并根据此分组找到数字的总和。然后,使用内部连接将此总和列添加到我们刚刚创建的主要大型数据框,即sticky.list.combined.df。

sticky.list.combined.df.sum<- sticky.list.combined.df %>%
  group_by(date, block, morphotype) %>%
  summarize(sum = sum(number))

# A tibble: 855 x 4
# Groups:   date, block [?]
   date            block morphotype    sum
   <fct>           <fct> <chr>       <dbl>
 1 date1 a     insect1     0
 2 date1 a     insect2     0
 3 date1 a     insect3     0
 4 date1 a     insect4     0
# … with 845 more rows

然后

sticky.list.analysis<-left_join(sticky.list.combined.df,sticky.list.combined.df.sum, by=c("date"="date",
                                                                                          "morphotype"="morphotype")) 

这是仅显示昆虫 1 的输出示例。是否保留每个 block.x 的 5 行的决定因素是最后两列 block.y 和 sum,它们表示每个块 (ad) 为化学物质 1-5 捕获的所有昆虫的总和。

      site       block.x    treatment date    number     morphotype block.y sum
1   location1       a       chemical1 date1      0         insect1       a   2
2   location1       a       chemical1 date1      0         insect1       b   8
3   location1       a       chemical1 date1      0         insect1       c   4
4   location1       a       chemical1 date1      0         insect1       d   0
5   location1       a       chemical2 date1      0         insect1       a   2
6   location1       a       chemical2 date1      0         insect1       b   8
7   location1       a       chemical2 date1      0         insect1       c   4
8   location1       a       chemical2 date1      0         insect1       d   0
9   location1       a       chemical3 date1      0         insect1       a   2
10  location1       a       chemical3 date1      0         insect1       b   8
11  location1       a       chemical3 date1      0         insect1       c   4
12  location1       a       chemical3 date1      0         insect1       d   0
13  location1       a       chemical4 date1      0         insect1       a   2
14  location1       a       chemical4 date1      0         insect1       b   8
15  location1       a       chemical4 date1      0         insect1       c   4
16  location1       a       chemical4 date1      0         insect1       d   0
17  location1       a       chemical5 date1      0         insect1       a   2
18  location1       a       chemical5 date1      0         insect1       b   8
19  location1       a       chemical5 date1      0         insect1       c   4
20  location1       a       chemical5 date1      0         insect1       d   0

这就是我认为我遇到的问题出现的地方

过滤总和 > 0 的行。

对于捕获日期(例如 date1)和形态类型的每个组合,删除在该块中具有零捕获的形态类型的行(即块 a-d)。在诱捕实验中(在汉克斯实验室统计实践中很常见)通常会丢弃或不包括没有捕获目标昆虫的日期。这可能与非生物因素(例如,太冷/太热、下雨)或与昆虫相关的物候因素有关。在我们的数据中保留这些零会降低我们在数据中发现显着影响的机会,因此我们将排除它们。

sticky.list.analysis.reduced<- sticky.list.analysis %>% 
  filter(sum > 0)

下面的缩短输出表明,对于昆虫 1,我们应该保留 a-c 块。保留哪些块将根据正在查看的昆虫而有所不同。我现在要做的是从 block.y 中获取这些数据并使用它来删除这些块的行。

不幸的是,这不是我想要的输出。 R 根据 sum 列删除了一行。我们现在看到根据 block.y 列删除了块 d。不幸的是,我们需要删除第 46-60 行。

输出:

       site block.x treatment date number morphotype block.y sum
1    location1   a    chemical1 date1   0      insect1    a   2
2    location1   a    chemical1 date1   0      insect1    b   8
3    location1   a    chemical1 date1   0      insect1    c   4
4    location1   a    chemical2 date1   0      insect1    a   2
5    location1   a    chemical2 date1   0      insect1    b   8
6    location1   a    chemical2 date1   0      insect1    c   4
7    location1   a    chemical3 date1   0      insect1    a   2
8    location1   a    chemical3 date1   0      insect1    b   8
9    location1   a    chemical3 date1   0      insect1    c   4
10   location1   a    chemical4 date1   0      insect1    a   2
11   location1   a    chemical4 date1   0      insect1    b   8
12   location1   a    chemical4 date1   0      insect1    c   4
13   location1   a    chemical5 date1   0      insect1    a   2
14   location1   a    chemical5 date1   0      insect1    b   8
15   location1   a    chemical5 date1   0      insect1    c   4
16   location1   b    chemical1 date1   0      insect1    a   2
17   location1   b    chemical1 date1   0      insect1    b   8
18   location1   b    chemical1 date1   0      insect1    c   4
19   location1   b    chemical2 date1   0      insect1    a   2
20   location1   b    chemical2 date1   0      insect1    b   8
21   location1   b    chemical2 date1   0      insect1    c   4
22   location1   b    chemical3 date1   0      insect1    a   2
23   location1   b    chemical3 date1   0      insect1    b   8
24   location1   b    chemical3 date1   0      insect1    c   4
25   location1   b    chemical4 date1   0      insect1    a   2
26   location1   b    chemical4 date1   0      insect1    b   8
27   location1   b    chemical4 date1   0      insect1    c   4
28   location1   b    chemical5 date1   0      insect1    a   2
29   location1   b    chemical5 date1   0      insect1    b   8
30   location1   b    chemical5 date1   0      insect1    c   4
31   location1   c    chemical1 date1   0      insect1    a   2
32   location1   c    chemical1 date1   0      insect1    b   8
33   location1   c    chemical1 date1   0      insect1    c   4
34   location1   c    chemical2 date1   0      insect1    a   2
35   location1   c    chemical2 date1   0      insect1    b   8
36   location1   c    chemical2 date1   0      insect1    c   4
37   location1   c    chemical3 date1   0      insect1    a   2
38   location1   c    chemical3 date1   0      insect1    b   8
39   location1   c    chemical3 date1   0      insect1    c   4
40   location1   c    chemical4 date1   0      insect1    a   2
41   location1   c    chemical4 date1   0      insect1    b   8
42   location1   c    chemical4 date1   0      insect1    c   4
43   location1   c    chemical5 date1   0      insect1    a   2
44   location1   c    chemical5 date1   0      insect1    b   8
45   location1   c    chemical5 date1   0      insect1    c   4
46   location1   d    chemical1 date1   0      insect1    a   2
47   location1   d    chemical1 date1   0      insect1    b   8
48   location1   d    chemical1 date1   0      insect1    c   4
49   location1   d    chemical2 date1   0      insect1    a   2
50   location1   d    chemical2 date1   0      insect1    b   8
51   location1   d    chemical2 date1   0      insect1    c   4
52   location1   d    chemical3 date1   0      insect1    a   2
53   location1   d    chemical3 date1   0      insect1    b   8
54   location1   d    chemical3 date1   0      insect1    c   4
55   location1   d    chemical4 date1   0      insect1    a   2
56   location1   d    chemical4 date1   0      insect1    b   8
57   location1   d    chemical4 date1   0      insect1    c   4
58   location1   d    chemical5 date1   0      insect1    a   2
59   location1   d    chemical5 date1   0      insect1    b   8
60   location1   d    chemical5 date1   0      insect1    c   4

期望的输出:

       site block.x treatment date number morphotype block.y sum
1    location1   a    chemical1 date1   0      insect1    a   2
2    location1   a    chemical1 date1   0      insect1    b   8
3    location1   a    chemical1 date1   0      insect1    c   4
4    location1   a    chemical2 date1   0      insect1    a   2
5    location1   a    chemical2 date1   0      insect1    b   8
6    location1   a    chemical2 date1   0      insect1    c   4
7    location1   a    chemical3 date1   0      insect1    a   2
8    location1   a    chemical3 date1   0      insect1    b   8
9    location1   a    chemical3 date1   0      insect1    c   4
10   location1   a    chemical4 date1   0      insect1    a   2
11   location1   a    chemical4 date1   0      insect1    b   8
12   location1   a    chemical4 date1   0      insect1    c   4
13   location1   a    chemical5 date1   0      insect1    a   2
14   location1   a    chemical5 date1   0      insect1    b   8
15   location1   a    chemical5 date1   0      insect1    c   4
16   location1   b    chemical1 date1   0      insect1    a   2
17   location1   b    chemical1 date1   0      insect1    b   8
18   location1   b    chemical1 date1   0      insect1    c   4
19   location1   b    chemical2 date1   0      insect1    a   2
20   location1   b    chemical2 date1   0      insect1    b   8
21   location1   b    chemical2 date1   0      insect1    c   4
22   location1   b    chemical3 date1   0      insect1    a   2
23   location1   b    chemical3 date1   0      insect1    b   8
24   location1   b    chemical3 date1   0      insect1    c   4
25   location1   b    chemical4 date1   0      insect1    a   2
26   location1   b    chemical4 date1   0      insect1    b   8
27   location1   b    chemical4 date1   0      insect1    c   4
28   location1   b    chemical5 date1   0      insect1    a   2
29   location1   b    chemical5 date1   0      insect1    b   8
30   location1   b    chemical5 date1   0      insect1    c   4
31   location1   c    chemical1 date1   0      insect1    a   2
32   location1   c    chemical1 date1   0      insect1    b   8
33   location1   c    chemical1 date1   0      insect1    c   4
34   location1   c    chemical2 date1   0      insect1    a   2
35   location1   c    chemical2 date1   0      insect1    b   8
36   location1   c    chemical2 date1   0      insect1    c   4
37   location1   c    chemical3 date1   0      insect1    a   2
38   location1   c    chemical3 date1   0      insect1    b   8
39   location1   c    chemical3 date1   0      insect1    c   4
40   location1   c    chemical4 date1   0      insect1    a   2
41   location1   c    chemical4 date1   0      insect1    b   8
42   location1   c    chemical4 date1   0      insect1    c   4
43   location1   c    chemical5 date1   0      insect1    a   2
44   location1   c    chemical5 date1   0      insect1    b   8
45   location1   c    chemical5 date1   0      insect1    c   4

一旦这个问题得到解决,我想从其列中对每个昆虫进行子集化(我知道如何手动执行此操作,但不是针对所有昆虫物种,但这是一个完全不同的问题)然后运行广义线性混合模型评估处理对捕获每只昆虫的影响,将日期和位置作为随机效应。

感谢您对此事的任何见解。如果我需要编辑此内容以添加任何其他信息,请告诉我,我已尽我所能使我的数据结构和问题清晰。谢谢。

【问题讨论】:

    标签: r list dataframe dplyr subset


    【解决方案1】:

    您尝试过subset 功能吗?它在base R 包(link)下定义。

    您可以执行以下操作:

    filtered.sticky.list.analysis <- subset(sticky.list.analysis, block.x == "a" || block.x == "b" || block.x == "c")
    

    另外一个可行的方法是:

    filtered.sticky.list.analysis <- subset(sticky.list.analysis, block.x != "d")
    
    

    代码很清楚。第一个选项选择block.x 等于abc 的所有内容。第二个选项选择与d 不同的所有内容。

    【讨论】:

    • Iago,感谢您的快速回复。不幸的是,这只是我整个数据集的一部分。正如您所建议的那样手动设置子集的问题是,在同一个昆虫中,但在不同的日期,或者对于不同昆虫的同一日期,我需要保留不同的块,所以我不一定只想在整个过程中保留这些块数据集。如果我的数据集较小,我可能会尝试手动对列进行子集化。不幸的是,数据集的大小使得这有点不合理。希望这可以澄清,我感谢您的洞察力。
    猜你喜欢
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 2017-11-09
    • 2021-08-16
    • 1970-01-01
    • 2016-06-18
    相关资源
    最近更新 更多