2

I have this string:

x <- c("A B B C")

[1] "A B B C"

I am looking for the shortest way to get this:

[1] "A B C"

I have tried this: Removing duplicate words in a string in R

paste(unique(x), collapse = ' ')

[1] "A B B C"
# does not work

Background: In a dataframe column I want to count only the unique word counts.

TarJae
  • 72,363
  • 6
  • 19
  • 66

4 Answers4

5

A regex based approach could be shorter - match the non-white space (\\S+) followed by a white space character (\\s), capture it, followed by one or more occurrence of the backreference, and in the replacement, specify the backreference to return only a single copy of the match

gsub("(\\S+\\s)\\1+", "\\1", x)
[1] "A B C"

Or may need to split the string with strsplit, unlist, get the unique and then paste

paste(unique(unlist(strsplit(x, " "))), collapse = " ")
# [1] "A B C"
akrun
  • 874,273
  • 37
  • 540
  • 662
3

Another possible solution, based on stringr::str_split:

library(tidyverse)

str_split(x, " ") %>% unlist %>% unique

#> [1] "A" "B" "C"
PaulS
  • 21,159
  • 2
  • 9
  • 26
3

Just in case the duplicates are not following each other, also using gsub.

x <- c("A B B C")
gsub("\\b(\\S+)\\s+(?=.*\\b\\1\\b)", "", x, perl=TRUE)
#[1] "A B C"

gsub("\\b(\\S+)\\s+(?=.*\\b\\1\\b)", "", "A B B A ABBA", perl=TRUE)
#[1] "B A ABBA"
GKi
  • 37,245
  • 2
  • 26
  • 48
2

You can use ,

gsub("\\b(\\w+)(?:\\W+\\1\\b)+", "\\1", x)
Mohamed Desouky
  • 4,340
  • 2
  • 4
  • 19