Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I use str_detect() in a function to create new columns?

I have a dataset with a column of freeform text e.g.

dat <– tribble(
  ~id, ~freeform_text,
  1, "some words to detect from",
  2, "some more words to detect"
)

I want to create a function that takes a specified dataframe, searches a column for a given string and returns a new column indicating whether the string was detected.

I would like to do this using tidyverse syntax.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is what I have tried so far…

create_text_feature <- function(data, column, string) {
  data %>% 
    mutate("{{string}}_detected" := ifelse(str_detect({{column}}, string), 1, 0))
}

Ideally, I would then run create_text_feature(dat, freeform_text, more) and I would end up with the following dataset.

dat <– tribble(
  ~id, ~freeform_text, ~more_detected,
  1, "some words to detect from", 0,
  2, "some more words to detect", 1
)

I would be even more grateful if this could be created to take a list of strings and create multiple new columns in the same way.

>Solution :

You could achieve your result by apssing the pattern as a quoted string and use single curly braces in the assignment:

library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 4.1.2
#> Warning: package 'tibble' was built under R version 4.1.2
#> Warning: package 'tidyr' was built under R version 4.1.2
#> Warning: package 'readr' was built under R version 4.1.2
#> Warning: package 'dplyr' was built under R version 4.1.2

dat <- tribble(
  ~id, ~freeform_text,
  1, "some words to detect from",
  2, "some more words to detect"
)

create_text_feature <- function(data, column, string) {
  data %>% 
    mutate("{string}_detected" := ifelse(str_detect({{column}}, string), 1, 0))
}


create_text_feature(dat, freeform_text, "more")
#> # A tibble: 2 x 3
#>      id freeform_text             more_detected
#>   <dbl> <chr>                             <dbl>
#> 1     1 some words to detect from             0
#> 2     2 some more words to detect             1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading