Creating binary column if start of phrase is present in row

I am relatively new to R and trying to create a binary column at the end of my data depending on if a particular phrase is present in a row. I have a vector with the start of the phrase . So for example I have chf <-("I1", "B4"). If there is a phrase in the row in a particular set of columns "B421" then I would want there to be a one in a new column, if there is no phrase beginning with B4 or I1 then a 0 in that column.

I have attached

I have written some code as below but it appears with an error, also below:



data <- data %>% 
  mutate(
    CHF = case_when(
      if_any(.cols = starts_with("a"), 
             .fns = ~stringr::str_detect(.x, “^chf”)) ~ 1, 
      TRUE ~ 0)

Error: unexpected input in:
"      if_any(.cols = starts_with("a"), 
             .fns = ~stringr::str_detect(.x, “^chf"
>       TRUE ~ 0)
Error: unexpected ')' in "      TRUE ~ 0)"

The desired outcome would look like:


#>   person    a1    a2    a3       chf
#>     <int> <chr>  <chr>  <chr>    <dbl>
#> 1       1   a     I1021  a         1
#> 2       2   b      b     B4231     1
#> 3       3   B4221  c     c         1
#> 4       4   d      d     d         0
#> 5       5   e      e     e         0

Any help is adjusting this so it works would be really appreciated.

The problems I found with the code you posted are:
There was no ) to close the mutate statement.
The quotes in str_detect(.x, “^chf”) are curly quotes. They need to be straight, like ".
Using "^chf" in str_detect() will search for cells beginning with the literal chf. If you want to use a variable in str_detect(), you need to not use quotes around it.
This seems to work.

library(tibble)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
data <- tribble(
  ~person,  ~a1,  ~a2,  ~a3,
   "1", "a",  "I1021",  "a",  
   "2", "b",  "b",  "B4231",  
   "3", "B4221",  "c",  "c",  
   "4", "d",  "d",  "d",  
   "5", "e",  "e",  "e",  
)
chf <- c("I1", "B4")
chf <- paste(chf, collapse = "|")
chf <- paste0("^", chf)
chf
#> [1] "^I1|B4"

data <- data %>% 
  mutate(
    CHF = case_when(
      if_any(.cols = starts_with("a"), 
             .fns = ~stringr::str_detect(.x, chf)) ~ 1, 
      TRUE ~ 0))

data
#> # A tibble: 5 x 5
#>   person a1    a2    a3      CHF
#>   <chr>  <chr> <chr> <chr> <dbl>
#> 1 1      a     I1021 a         1
#> 2 2      b     b     B4231     1
#> 3 3      B4221 c     c         1
#> 4 4      d     d     d         0
#> 5 5      e     e     e         0

Created on 2021-12-30 by the reprex package (v2.0.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.