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