Return only the unique words

Viewed 65

Lets say i have a string and i only want unique words in the sentence as separate elements

 a = "an apple is an apple"
word <- function(a){
  
  words<- c(strsplit(a,split = " "))
  return(unique(words))
}

word(a)

This returns

[[1]]
[1] "an"    "apple" "is"    "an"    "apple"

and the output im expecting is

'an','apple','is'

what im doing wrong? really appreciate any help

Cheers!

3 Answers

Another possible solution, based on stringr::str_split:

library(tidyverse)

a %>% str_split("\\s+") %>% unlist %>% unique

#> [1] "an"    "apple" "is"

The problem is that wrapping strsplit(.) in c(.) does not change the fact that it is still a list, and unique will be operating at the list-level, not the word-level.

c(strsplit(rep(a, 2), "\\s+"))
# [[1]]
# [1] "an"    "apple" "is"    "an"    "apple"
# [[2]]
# [1] "an"    "apple" "is"    "an"    "apple"
unique(c(strsplit(rep(a, 2), "\\s+")))
# [[1]]
# [1] "an"    "apple" "is"    "an"    "apple"

Alternatives:

  1. If length(a) is always 1, then perhaps

    unique(strsplit(a, "\\s+")[[1]])
    # [1] "an"    "apple" "is"   
    
  2. If length(a) can be 2 or more and you want a list of unique words for each sentence, then

    a2 <- c("an apple is an apple", "a pear is a pear", "an orange is an orange")
    lapply(strsplit(a2, "\\s+"), unique)
    # [[1]]
    # [1] "an"    "apple" "is"   
    # [[2]]
    # [1] "a"    "pear" "is"  
    # [[3]]
    # [1] "an"     "orange" "is"    
    

    (Note: this always returns a list, regardless of the number of sentences in the input.)

  3. if length(a) can be 2 ore more and you want a unique words across all sentences, then

    unique(unlist(strsplit(a2, "\\s+")))
    # [1] "an"     "apple"  "is"     "a"      "pear"   "orange"
    

    (Note: this method also works well when length(a) is 1.)

You could try

unique(unlist(strsplita, " ")))
Related