I am trying to recode a character variable with dplyr::recode()
and stringr::str_detect()
. I realize that this can be done with dplyr::case_when()
, as documented here: https://community.rstudio.com/t/recoding-using-str-detect/5141, but I am convinced that there has to be a way of doing it via recode()
.
Consider this case:
library(tidyverse)
rm(list = ls())
data <- tribble(
~id, ~time,
#--|--|
1, "a",
2, "b",
3, "x"
)
I would like to replace the "x" in the dataframe with a "c" via str_detect()
and here's how I'd do it:
data %>%
mutate(time = recode(data$time, str_detect(data$time, "x") = "c"))
But that doesn't work:
Error: unexpected '=' in: "data %>% mutate(time = recode(data$time, str_detect(data$time, "x") ="
Apparently R doesn't know what to do with the last =
, but I believe it has to be there for the recode function, as demonstrated here:
recode(data$time, "x" = "c")
This executes properly, as does this:
str_detect(data$time, "x")
But this does not:
recode(data$time, str_detect(data$time, "x") = "c")
Is there a way of getting these two functions to work with each other?