Search code examples
rdplyrdrop

R: How to drop the corresponding rows if there is a 1 in columns with a column name containing a specific string


I would like to drop a whole row if there is a 1 in any column having a name that ends with "rem" or "nocut". Sorry, I can't list the whole data set here because it's too large. Here is an example of my data:

Sequence| #1C_cut  | #2C_nocut | #1C_rem | #3C_cut | #3C_rem | #4C_nocut|
R1      |    0     |     0     |    1    |    1    |    0    |     0    |
R2      |    0     |     1     |    0    |    0    |    0    |     1    |
R3      |    1     |     0     |    0    |    0    |    0    |     0    |
R4      |    0     |     0     |    0    |    1    |    0    |     0    |
R5      |    1     |     0     |    0    |    1    |    0    |     0    |

What I want:

Sequence| #1C_cut  | #2C_nocut | #1C_rem | #3C_cut | #3C_rem | #4C_nocut|
R3      |    1     |     0     |    0    |    0    |    0    |     0    |
R4      |    0     |     0     |    0    |    1    |    0    |     0    |
R5      |    1     |     0     |    0    |    1    |    0    |     0    |

I have an idea of combining select() and filter() function in dplyr to solve this problem, but I don't know how to do that......


Solution

  • Borrowing the idea from @jrcalabrese. Without manipulating the data (mutate()), you can filter() as such:

    df %>%  
      filter(if_all(ends_with(c("_rem", "_nocut")), ~ .x != 1))
    
    # A tibble: 3 × 7
      Sequence `1C_cut` `2C_nocut` `1C_rem` `3C_cut` `3C_rem` `4C_nocut`
      <chr>       <dbl>      <dbl>    <dbl>    <dbl>    <dbl>      <dbl>
    1 R3              1          0        0        0        0          0
    2 R4              0          0        0        1        0          0
    3 R5              1          0        0        1        0          0