0

I want to use a function to search for a word (or words) in a column and create a dummy variable that is 1 if a word is present and 0 if it is absent. The lines below yield no error message but also no result. What am I doing wrong? Adding !! and := does not seem to help. Thanks in advance.

library(tidyverse)

Sentences <- c("me love brown cookies", "I like blue and green", "c is for cookie", "I am fuzzy and blue")
df <- as.data.frame(Sentences)

# Define function 
FindWordMakeDummy <- function(VarName, SearchWord) {
  df <- df %>% 
    mutate(VarName = (as.integer(as.logical(str_detect(Sentences, SearchWord)))))
}

# Apply function 
FindWordMakeDummy("Cookies", "cookie")
FindWordMakeDummy("Color", "green|blue|brown")
Lidwien
  • 11
  • 3
  • You've just encountered tidyverse's non-standard evaluation [NSE](https://dplyr.tidyverse.org/articles/programming.html). Remember to use `:=` rather than `=` in your assignment because your argument is on the left hand side of the assignment. – Limey May 10 '22 at 11:07
  • Does this answer your question? [dplyr NSE - how pass column names to mutate function call?](https://stackoverflow.com/questions/46286666/dplyr-nse-how-pass-column-names-to-mutate-function-call) – Limey May 10 '22 at 11:09
  • ... and your function returns `NULL` because its final statement assigns a value to a local variable. Delete the `df <- `. – Limey May 10 '22 at 11:12

1 Answers1

2

Non-standard evaluation is great when you're working interactively but a nightmare in functions - or at least it confuses me every time. The walrus operator in the function body expresses my feelings about this syntax := (.

Anyway, you can do it like this:

# Define function 
FindWordMakeDummy <- function(VarName, SearchWord) {
  df <- df %>% 
    mutate(
        "{{VarName}}" := (
            as.integer(
                str_detect(Sentences, SearchWord)
            )
        )
    )
    df
}

FindWordMakeDummy(Cookies, "cookie")

#               Sentences Cookies
# 1 me love brown cookies       1
# 2 I like blue and green       0
# 3       c is for cookie       1
# 4   I am fuzzy and blue       0

I also removed the as.logical() from the function body as str_detect() is equivalent to grepl(), i.e. it returns a logical vector.

SamR
  • 8,826
  • 3
  • 11
  • 33