Trouble adding new variable to dataframe based on grepl function output

Hello all,

I'm trying to create a new variable in a dataframe (d0) based on the grepl output on an existing string variable (song_input). However, neither of the 2 options that I tried below work and I'm not sure why. Can anyone help to shed any light?

d0 = mutate(d0, HasShapeOfYou = ifelse(grepl('shape of you', song_input, ignore.case = TRUE), "Yes", "No"))

d0$HasShapeOfYou = grepl('shape of you', d0$song_input, ignore.case = TRUE)

I think your approach works, but be sure there are no extra spaces in song_input prior to mutating. Below is an example that removes extra spaces using stringr::str_squish().

library(dplyr)

# sample data
d0 = data.frame(song_input = c('Test statement',
                               'Test statement Shape  of you'))

# notice extra space in row 2
d0
#>                     song_input
#> 1               Test statement
#> 2 Test statement Shape  of you

# grepl
d1 = mutate(d0, HasShapeOfYou = ifelse(grepl('shape of you', song_input, ignore.case = TRUE), 
                                       "Yes", "No"))
d1
#>                     song_input HasShapeOfYou
#> 1               Test statement            No
#> 2 Test statement Shape  of you            No


# grepl after removing extra space with str_squish
d0$song_input = stringr::str_squish(d0$song_input)
d2 = mutate(d0, HasShapeOfYou = ifelse(grepl('shape of you', song_input, ignore.case = TRUE), 
                                       "Yes", "No"))
d2
#>                    song_input HasShapeOfYou
#> 1              Test statement            No
#> 2 Test statement Shape of you           Yes

Created on 2023-04-11 with reprex v2.0.2

Hi @scottyd22,

Thank you for your suggestion and the example code.

I tried that but there's still no extra column in the d0 dataframe with the 'HasShapeOfYou' variable. I noticed quite a few rows for each participant have 'NA' values in the song_input column and wonder if that has something to do with it?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.