Removing a row based on a condition

  my_df <- tibble(
    b1 = c(2, 1, 1, 2, 2, 2, 1, 1, 1), 
    b2 = c(NA, 4, 6, 2, 6, 6, 1, 1, 7), 
    b3 = c(5, 9, 8, NA, 2, 3, 9, 5, NA), 
    b4 = c(NA, 6, NA, 10, 12, 8, 3, 6, 2),
    b5 = c(2, 12, 1, 7, 8, 5, 5, 6, NA),
    b6 = c(9, 2, 4, 6, 7, 6, 6, 7, 9),
    b7 = c(1, 3, 7, 7, 4, 2, 2, 9, 5),
    b8 = c(NA, 8, 4, 5, 1, 4, 1, 3, 6),
    b9 = c(4, 5, NA, 9, 5, 1, 1, 2, NA),
    b10 = c(14, 3, 8, 2, 2, 2, 3, NA, 5))
 

I have a df like this, and would like to tell R to remove all '3' , '5' and 'NA' in b10 if b1 = 1. However, where b1 is not 1, leave untouched. I have tried this, but the result seems wrong. Any help please.

library(dplyr)
my_df %>% 
  filter(!(b1 == 1 & (is.na(b10) | b10 == 3 |  5)))
my_df %>% 
  filter( ! b10 %in% c(3,5,NA)  | b1 != 1)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.