Filter dataset with column not like any value in a list

Viewed 29

I want to filter a dataset with a column not like any value in a list

I tried different ways but they don't work


month <- c("2022-02", "2022-02", "2022-03", "2022-03", "2022-05","2022-06", "2022-07","2022-02", "2022-04","2022-5","2022-6","2022-7")
total_amount <- c(100, 200, 500, 600, 700, 300, 500, 900, 900, 600, 500, 700)
merchant_name <- c("fpay", "gpay", "upay", "zpay","tpay","spay","mpay", "dpay", "bpay", "opay", "npay", "ypay")

df <- data.frame(month, total_amount, merchant_name)
a <- c("fpay", "gpay", "upay", "zpay","tpay","spay","mpay")
df1 = df %>% list.filter(!merchant_name %like% a)

or

df1 = subset(df, !merchant_name%like% a)

or

df1 <- df[apply(df,1,function(x) sum(!x %like% a)>=1),]

However, the number of observations of df1 is the same to df's, not filter at all. And I receive a warning message:

In grepl(pattern, vector, ignore.case = ignore.case, fixed = fixed) : argument 'pattern' has length > 1 and only the first element will be used

Please kindly help me. Thanks a lot

2 Answers

If these are substring, use grepl after pasteing the elements in 'a' with collapse = "|"

subset(df, grepl(paste(a, collapse = "|"), merchant_name))

Or using %like%

subset(df, Reduce(`|`, lapply(a, \(x) merchant_name %like% x)))

Simply do:

df[! df$merchant_name %in% a, ]

To understand what this code, let's work our way inside out.

First, the expression df$merchant_name %in% a. This creates a logical vector (a vector of TRUE or FALSE values) with an entry for each element in df$merchant_name (and hence for each row in df). The corresponding entry for each row is TRUE if that entry is in a and FALSE otherwise (technically there are special cases if the entry itself is NA).

Then, ! df$merchant_name %in% a negates the previous expression. This matches our intent to find those rows that don't match the list.

Finally, we can use the previous expression to subset/filter the df for the desired entries. To do this, we'll use the previous expression as an index vector for the row position (e.g. In df[i, j], i is the index vector for the row-position and j is an index vector for the column-position.). Bringing us full circle:

df[! df$merchant_name %in% a, ]
Related