Lidwien
Lidwien

Reputation: 11

I want to use mutate in a function

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")

Upvotes: 0

Views: 46

Answers (1)

SamR
SamR

Reputation: 20425

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.

Upvotes: 2

Related Questions