-1

I am trying to clean up the strings in a column in my R dataframe but ran into a problem where I am running too many ifelse statements (it is telling me that the maximum is 50). What is the alternative to this? Put simply I want to detect a string in one column and give an output in another column, however there are more than 50 titles. what is the alternative to this method?

for example:

ifelse(str_detect(titles$program_name, "¿sabías que...?") == TRUE, "¿sabías que...?",
ifelse(str_detect(titles$program_name, "12 corazones|12 hearts|2 hearts") == TRUE, "12 Corazones",
ifelse(str_detect(titles$program_name, " al lado del otro muro") == TRUE, "Al Otro Lado Del Muro",
ifelse(str_detect(titles$program_name, "a toda gloria") == TRUE, "A Toda Gloria", 
ifelse(str_detect(titles$program_name, "acceso vip") == TRUE, "Acceso VIP",
ifelse(str_detect(titles$program_name, "afv") == TRUE, "AFV",
ifelse(str_detect(titles$program_name, "al rojo vivo") == TRUE, "Al Rojo Vivo",
ifelse(str_detect(titles$program_name, "alma awards") == TRUE, "ALMA Awards",
ifelse(str_detect(titles$program_name, "amar es primavera") == TRUE, "Amar es primavera",
ifelse(str_detect(titles$program_name, "anónima|anonima|anonymized") == TRUE, "Anonima",
ifelse(str_detect(titles$program_name, "astrologia") == TRUE, "Astrologia",
ifelse(str_detect(titles$program_name, "billboard") == TRUE, "Premios Billboard",
ifelse(str_detect(titles$program_name, "bajo el mismo cielo") == TRUE, "Bajo El Mismo Cielo",
ifelse(str_detect(titles$program_name, "bella calamidades") == TRUE, "Bella Calamidades",
ifelse(str_detect(titles$program_name, "betty") == TRUE, "Betty en NY",
ifelse(str_detect(titles$program_name, "buscando mi ritmo") == TRUE, "Buscando Mi Ritmo",


### this continues on for more than 50 titles

how can i do this so as to avoid the error?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Michael Lopez
  • 79
  • 2
  • 11
  • I would say don't use `ifelse`. It's easier to suggest an alternative if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Sep 10 '19 at 19:35

1 Answers1

1

The following will work as long as each element of P matches with only one element of S

P = c("apple", "orange")
S = c("red apples", "green apple", "oranges", "some orange")

foo = function(p, s){
    s2 = s
    for (x in p) {
        s2 = replace(s2, grepl(x, s2), x)
    }
    return(s2)
}

foo(P, S)
#> [1] "apple"  "apple"  "orange" "orange"

OR

mypattern = paste0(".*(", paste(P, collapse = "|"), ").*")
mypattern
#> [1] ".*(apple|orange).*"

gsub(mypattern, "\\1", S)
#> [1] "apple"  "apple"  "orange" "orange"
d.b
  • 32,245
  • 6
  • 36
  • 77