0

I'm trying to use the billboard tibble in R to find all the songs titles that contain the word "Love" in them using string basics but I'm struggling.

The end result should look like this:

 [1]"I'm Outta Love"          "My First Love"
 [3] "My Love Goes On And ..." "He Loves U Not"
 [5] "Love Is Blind"           "I Will Love Again"      
 [7] "Feels Like Love"         "Let's Make Love" 
 [9] "My Love Is Your Love"    "It Must Be Love"        
 [11] "He Can't Love U"         "I Just Wanna Love U ..."
 [13] "U Don't Love Me"         "Don't Say You Love M..."
 [15] "Love's The Only Hous..." "Because You Love Me"    
 [17] "Love Sets You Free"      "I Knew I Loved You"     
 [19] "I Think I'm In Love ..." "Live, Laugh, Love"      
 [21] "The Chain Of Love"
Kieran
  • 11
  • 1

1 Answers1

1
data("billboard")

# as suggested by @Cath which is of course the way to go
grep("Love", billboard$track, value = TRUE)

# original answer
billboard$track[grepl("Love", billboard$track)]

#  [1] "I'm Outta Love"          "My First Love"           "My Love Goes On And ..." "He Loves U Not"          "Love Is Blind"          
#  [6] "I Will Love Again"       "Feels Like Love"         "Let's Make Love"         "My Love Is Your Love"    "It Must Be Love"        
# [11] "He Can't Love U"         "I Just Wanna Love U ..." "U Don't Love Me"         "Don't Say You Love M..." "Love's The Only Hous..."
# [16] "Because You Love Me"     "Love Sets You Free"      "I Knew I Loved You"      "I Think I'm In Love ..." "Live, Laugh, Love"      
# [21] "The Chain Of Love"  
Merijn van Tilborg
  • 5,452
  • 1
  • 7
  • 22
  • 1
    why not just `grep("Love", billboard$track, value=TRUE)`? (Not that as it's supposed to match a whole word, you might want to add word boundaries `\\b` around `Love`) – Cath Feb 23 '22 at 16:28
  • 1
    You are absolutely right! It makes no sense to use grepl and then subset again. – Merijn van Tilborg Feb 24 '22 at 08:10