using argument function with stringr::str_extract

Viewed 34

I'm trying to parse an argument from a custom function through stringr::str_extract but I'm not able to make it work.

Here's what I've tried so far:

library(tidyverse)

extr_var <- function(data, variable) {

      res <- data %>% 
      mutate(kk = str_extract(variable, 'setosa'))

      return(res)
}


extr_var(iris, "Species")

and the top 6 rows from the output:

#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species   kk
#1          5.1         3.5          1.4         0.2  setosa <NA>
#2          4.9         3.0          1.4         0.2  setosa <NA>
#3          4.7         3.2          1.3         0.2  setosa <NA>
#4          4.6         3.1          1.5         0.2  setosa <NA>
#5          5.0         3.6          1.4         0.2  setosa <NA>
#6          5.4         3.9          1.7         0.4  setosa <NA>

I've tried using match.call, substitute and other but I couldn't make it work. Any help is appreciated.

0 Answers
Related