create a logical column for whether a row contains a string in any column

Omry Atia

I have the following data frame:

df <- structure(list(x = c("cc", "aa", "BB", "dd"), y = c("ee", "dd",
"ff", "gg"), z = c("AA", "gg", "bb", "dd")), row.names = c(NA,
-4L), class = c("tbl_df", "tbl", "data.frame"))

I would like to create a binary column indicating whether each row contains "aa" (case insensitive) in any column. So in this case the first two values will be TRUE, and the last true will be FALSE. How can I do this using dplyr? all the answers explain how to filter those rows, rather than how to book-keep them

akrun

We could use a vectorized option with if_any

library(dplyr)
library(stringr)
df %>%
     mutate(xyz = +(if_any(everything(), 
         ~ str_detect(., regex('aa', ignore_case = TRUE)))))

-output

# A tibble: 4 x 4
  x     y     z       xyz
  <chr> <chr> <chr> <int>
1 cc    ee    AA        1
2 aa    dd    gg        1
3 BB    ff    bb        0
4 dd    gg    dd        0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Moving row values contains specific string to new column in Python

Setting flag column depending on whether column contains a given string

How to create a column that contains the penultimate value of each row?

In R, find the column that contains a string in for each row

What is the condition to check if a string contains any string of a column and vice versa?

If Column Contains String then enter value for that row

Remove row if any column contains a specific string

Remove values from dataframe if a column contains any string value in Pandas

Trying to recode a column based on whether or not it contains a certain string

How do I return a data frame with logical columns which denote whether or not a string occurs in a column?

how to check whether column of text contains specific string or not in pandas

Create a new column with a specific string in every row

Delete row if any column contains one of the keywords

Create new column if DataFrame contains specific string

R: To identify whether column names in a dataframe contains string

create new column based on whether another column contains each row name

How to get a row from a look up table column contains any

Excel -- Tell whether a string contains any value in a table column

Excel VBA: Delete row if Column A contains a number within the string

Pandas Dataframe Keep Row If Column Contains Any Designated Partial String

Is it possible to select rows if a string column contains any number value?

Find Exact Match Record and String Contains column 1 row word

Python Pandas Filter By Row Contains (at any column)

How to create a column contains words from a string?

How do I create a column that tells me whether or not another column contains alpha numeric values?

How to filter Pandas DataFrame by checking whether a Column contains any String from a List?

Delete rows where any column contains a certain string

New identifier column to dataframe based on whether string contains said identifier

How does one create a pandas column (binary) based on whether or not another column contains dates