Home > Software engineering >  Populate a column based on a pattern in another column
Populate a column based on a pattern in another column

Time:05-02

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.

CodePudding user response:

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"))

CodePudding user response:

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))

  • Related