rstringfiltercolumnsorting

Filtering by a column by string values from a different column


Let me introduce a similar problem to that I encountered

a <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
b <- c("Saturday", "Sunday")

I want to filter/remove values of b from a, meaning I only need to end up with 5 days. To complicate the matter, I have a df_1 with 3000 columns of data(strings) and another df_2 with 700 columns to filter accordingly. I need to end up with 3000 - 700 = 2300 values at best, but some from df_2 might not be in df_1! Please help my boiling brain. Thanks

I tried using:

str_detect
df_1 %>% filter(print(across(everything(), ~ !str_detect(.,df_2))))

no luck so far, the issue must be the method I am using


Solution

  • You can use setdiff

    setdiff(a, b)
    # [1] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"  
    

    Or by indexing

    a[!(a %in% b)]
    # [1] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"