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?
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
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?