0
votes

imagine I have a simple tibble:

tribble(~a,~b,~c,
        1, "def", "abc",
        2, "def", "def")

I want to mutate a new column "d" with values conditioned on whether a string exists in all other columns. In this case, I'm looking for the string "abc". The final output would look like:

tribble(~a,~b,~c,~d,
        1, "def", "abc", "present",
        2, "def", "def", "absent")

In reality my tibble has ~20 columns, of which maybe 10 are character, and the strings I'm looking for are more complex, like "[Aa]|[Cc]". I'm sure there's a simple-ish way with pmap,case_when and str_detect but can't work it out at all!

1

1 Answers

0
votes

Using rowSums in base R :

cols <- sapply(df, is.character)
df$d <- ifelse(rowSums(sapply(df[cols], grepl, pattern = 'a')) > 0, 
               'present', 'absent')

With dplyr, we can use rowwise with c_across :

library(dplyr)
library(stringr)

df %>%
  rowwise() %>%
  mutate(d = if(any(str_detect(c_across(where(is.character)), 'a'))) 
                'present' else 'absent')

#      a   b     c     d      
#  <dbl> <chr> <chr> <chr>  
#1     1 def   abc   present
#2     2 def   def   absent