Removing duplicate words in a string in R

后端 未结 4 1080
栀梦
栀梦 2020-12-11 03:47

Just to help someone who\'s just voluntarily removed their question, following a request for code he tried and other comments. Let\'s assume they tried something like this:

4条回答
  •  有刺的猬
    2020-12-11 04:05

    There are no need additional package

    str <- c("How do I best try and try and try and find a way to to improve this code?",
             "And and here's a second one one and not a third One.")
    

    Atomic function:

    rem_dup.one <- function(x){
      paste(unique(tolower(trimws(unlist(strsplit(x,split="(?!')[ [:punct:]]",fixed=F,perl=T))))),collapse = " ")
    }
    rem_dup.one("And and here's a second one one and not a third One.")
    

    Vectorize

    rem_dup.vector <- Vectorize(rem_dup.one,USE.NAMES = F)
    rem_dup.vector(str)
    

    REsult

    "how do i best try and find a way to improve this code" "and here's a second one not third" 
    

提交回复
热议问题