I have a dataset of character variables:
col1 = c("a","b","c")
col2 = c("a","b_a","d")
df = data.frame(col1,col2)
col1 col2
1 a a
2 b b_a
3 c d
I want to create a variable a that flags 1,0 if any value in that row contains the substring "a".
col1 col2 a
1 a a 1
2 b b_a 1
3 c d 0
My attempt is below. It doesn't quite do it, as I believe it takes TRUE
if any value in the dataframe contains the substring, rather than the row.
df["a"] = ifelse(any(sapply(df,function(x) str_detect(x,"a")),TRUE),1,0)
My thinking was that with an ifelse
statement, any functions within the ifelse
statement only evaluates df[i,]
rather than the entire dataframe where i
is the row it is looking at. This doesn't seem to be the case.
How do I construct the data frame I'm looking for? Note that in my real dataset, there are 100+ columns, so it doesn't make sense to list them all out.
Why doesn't ifelse only evaluate row
i
ofdf
, rather than the wholedf
?
Note that previous questions only look at one variable, I am looking at all variables so this is not a duplicate.