带dplyr的条件过滤器

时间:2019-05-10 08:20:52

标签: r dplyr

这是一个数据框:

  cluster_names Species values   Nsp Nsp_MRCA Event NB_Event Nsp_losses
 1 Group1        Sp1          1     3        3     1        2          0
 2 Group1        Sp1          4     3        3     1        2          0
 3 Group1        Sp2         78    NA       NA     1        2         NA
 4 Group1        Sp3         NA     3       12     2        2          9
 5 Group1        Sp4         NA     3        3     2        2          0
 6 Group2        Sp2          3     2        3     2        2          1
 7 Group2        Sp3          9     2       40     2        2         38
 8 Group2        Sp4          8    NA       NA     2        2         NA
 9 Group3        Sp1          9     2        2     1        1          0
10 Group3        Sp3         10     3        3     1        1          0
11 Group3        Sp3         12     3       20     1        1         17
12 Group3        Sp3         14     2        3     1        1          1
13 Group4        Sp4         23     3      112     1        1        109
14 Group5        Sp3         34     5      114     1        1        109
15 Group6        Sp4          2     3        3     1        1          0

我怎么说deplyr,只保留Groups,其中:

  • Nsp > 1至少持续one row
  • Nsp == Nsp_MRCA至少一行
  • 如果所有Nsp在5和2之间,并且所有Nsp_losses < 3在所有Nsp_losses < 20
  • 所有NB_Event必须是< 3

使用这种过滤器,我应该得到一个新的df:

  cluster_names Species values   Nsp Nsp_MRCA Event NB_Event Nsp_losses
 1 Group1        Sp1          1     3        3     1        2          0
 2 Group1        Sp1          4     3        3     1        2          0
 3 Group1        Sp2         78    NA       NA     1        2         NA
 4 Group1        Sp3         NA     3       12     2        2          9
 5 Group1        Sp4         NA     3        3     2        2          0
 9 Group3        Sp1          9     2        2     1        1          0
10 Group3        Sp3         10     3        3     1        1          0
11 Group3        Sp3         12     3       20     1        1         17
12 Group3        Sp3         14     2        3     1        1          1
15 Group6        Sp4          2     3        3     1        1          0

详细信息:

  • Group1被保留是因为Nsp52之间并且所有Nsp_losses < 20
  • Group2被删除,因为Nsp_losses = 38
  • Group3被保留是因为Nsp52之间并且所有Nsp_losses < 20
  • Groups 45被删除,因为Nsp_losses = 38
  • Group6之所以保留,是因为Nsp == Nsp_MRCA至少有一行

所有这些至少有一行Nsp> 1

到目前为止,我尝试了以下代码:

tab %>%
  group_by(cluster_names) %>%
    mutate(NB_Event = max(Event,na.rm=TRUE))  %>%
    filter(any(Nsp > 1 |is.na(Nsp))) %>%
    filter(any(Nsp == Nsp_MRCA)) %>%
    mutate(Nsp_losses = abs(Nsp - Nsp_MRCA)) %>%
    filter(all(Nsp <=5 |is.na(Nsp)) & all(Nsp > 1 |is.na(Nsp) & all(Nsp_losses < 20 |is.na(Nsp_losses))))  %>%

这是数据框

structure(list(cluster_names = structure(c(1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 5L, 6L), .Label = c("Group1", 
"Group2", "Group3", "Group4", "Group5", "Group6"), class = "factor"), 
    Species = structure(c(1L, 1L, 2L, 3L, 4L, 2L, 3L, 4L, 1L, 
    3L, 3L, 3L, 4L, 3L, 4L), .Label = c("Sp1", "Sp2", "Sp3", 
    "Sp4"), class = "factor"), values = c(1L, 4L, 78L, NA, NA, 
    3L, 9L, 8L, 9L, 10L, 12L, 14L, 23L, 34L, 2L), Nsp = c(3L, 
    3L, NA, 3L, 3L, 2L, 2L, NA, 2L, 3L, 3L, 2L, 3L, 5L, 3L), 
    Nsp_MRCA = c(3L, 3L, NA, 12L, 3L, 3L, 40L, NA, 2L, 3L, 20L, 
    3L, 112L, 114L, 3L), Event = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), class = "data.frame", row.names = c(NA, 
-15L))

感谢您的帮助和时间。

3 个答案:

答案 0 :(得分:2)

我们可以做到:

     tab %>% 
      group_by(cluster_names) %>% 
      mutate(Nsp_losses = abs(Nsp - Nsp_MRCA),
Cond=ifelse(Nsp_losses < 20 & between(Nsp,2,5) || Nsp==Nsp_MRCA ,1,0)) %>% 
      filter(Cond==1) %>% 
     filter(all(Nsp_losses)<20)  %>% 
      select(-Cond)


 cluster_names Species values   Nsp Nsp_MRCA Event Nsp_losses
   <fct>         <fct>    <int> <int>    <int> <int>      <int>
 1 Group1        Sp1          1     3        3     1          0
 2 Group1        Sp1          4     3        3     1          0
 3 Group1        Sp2         78    NA       NA     1         NA
 4 Group1        Sp3         NA     3       12     2          9
 5 Group1        Sp4         NA     3        3     2          0
 6 Group3        Sp1          9     2        2     1          0
 7 Group3        Sp3         10     3        3     1          0
 8 Group3        Sp3         12     3       20     1         17
 9 Group3        Sp3         14     2        3     1          1
10 Group6        Sp4          2     3        3     1          0

答案 1 :(得分:2)

NA的存在使操作变得有些棘手,因此,我首先使用na.omit将其删除,然后找出满足给定条件的组(cluster_names),然后再找到{{1 }}基于此。

filter

答案 2 :(得分:1)

假设您已经拥有NB_EventNsp_losses变量,并逐行重新创建文本:

library(tidyverse)

dat %>%
  group_by(cluster_names) %>%
  filter(
      any(Nsp > 1, na.rm = T)         & 
      any(Nsp == Nsp_MRCA, na.rm = T) & 
      all(NB_Event < 3, na.rm = T)    &
      all(Nsp_losses < 3, na.rm = T)  | 
      all(
        between(na.omit(Nsp), 2, 5)   & 
        all(Nsp_losses < 20, na.rm = T)
        )
    ) %>%
  ungroup()

哪个输出:

# A tibble: 10 x 8
   cluster_names Species values   Nsp Nsp_MRCA Event NB_Event Nsp_losses
   <fct>         <fct>    <int> <int>    <int> <int>    <dbl>      <int>
 1 Group1        Sp1          1     3        3     1        2          0
 2 Group1        Sp1          4     3        3     1        2          0
 3 Group1        Sp2         78    NA       NA     1        2         NA
 4 Group1        Sp3         NA     3       12     2        2          9
 5 Group1        Sp4         NA     3        3     2        2          0
 6 Group3        Sp1          9     2        2     1        2          0
 7 Group3        Sp3         10     3        3     1        2          0
 8 Group3        Sp3         12     3       20     1        2         17
 9 Group3        Sp3         14     2        3     1        2          1
10 Group6        Sp4          2     3        3     1        2          0