0

I have a DF where I am trying to populate a column based on whether a pattern in a string exists.

A      B     
E3Y12
E3Y45
E3Y56
c1234
c56534
c3456

I would like to check if A contains the string 'E3Y' and populate the column B with "This one" or if it doesn't contain that pattern "That one"

I've tried the dplyr starts_with inside a case_when() and ifelse() statement with no avail since it has to be part of a select function.

a nony mouse
  • 89
  • 11

2 Answers2

3

You can use str_detect() to evaluate if a string contains a certain pattern and then using an ifelse is straightforward:

library(dplyr)
tibble( A = c(
"E3Y12",
"E3Y45",
"E3Y56",
"c1234",
"c56534",
"c3456")) %>% 
  mutate(B = ifelse(stringr::str_detect(A, "E3Y"), "This one", "That one"))
Julian
  • 6,586
  • 2
  • 9
  • 33
2

try:

library(dplyr)

df %>% 
  mutate(B = ifelse(grepl("E3Y", A), "This one", "That one"))

Output is:

# A tibble: 6 × 2
  A      B       
  <chr>  <chr>   
1 E3Y12  This one
2 E3Y45  This one
3 E3Y56  This one
4 c1234  That one
5 c56534 That one
6 c3456  That one

used

df <- structure(list(A = c("E3Y12", "E3Y45", "E3Y56", "c1234", "c56534", 
"c3456"), B = c(NA, NA, NA, NA, NA, NA)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -6L))

Stephan
  • 2,056
  • 1
  • 9
  • 20
  • I tend to gravitate more towards package functions, I often forget there are still some intuitive base options. Is there a particular reason in this case you would prefer grepl over str_detect suggested above? – a nony mouse May 02 '22 at 14:08
  • 1
    Have a look here: https://stackoverflow.com/questions/57412700/whats-the-difference-between-the-str-detect-function-in-stringer-and-grepl-and – Julian May 02 '22 at 14:12
  • 1
    you don't have to import `stringr` its just a simple logical test. – Stephan May 02 '22 at 14:12