-1

I have a character column in a data frame that contains two or more phrases, each of these phrases is separated by a &&. I would like R to not differentiate between phrase1 && phrase2 and phrase2 && phrase1. Any ideas how I could go about this?

Example of the output I'd like...

text = c("a && b", "c && d", "e && f", "d && c", "g && h", "f && e")
desired_result = c("a && b", "c && d", "e && f", "c && d", "g && h", "e && f")
df = data.frame(text, desired_result )
NMaguire
  • 19
  • 1
  • 8

1 Answers1

3
sapply(strsplit(text, " && "), function(x) paste(sort(x), collapse=" && "))

does what you're after. It works by splitting the string before/after the && part, sorting and then pasting back together.

Miff
  • 7,486
  • 20
  • 20