removing duplicate words in a row

狂风中的少年 提交于 2019-12-13 02:31:24

问题


I have a column in table as below

    Col1
    ========================
    "No","No","No","No","No"
    "No","No","No"
     Yes
     No
    "Yes","Yes","Yes","Yes"
    "Yes","No","Yes", "Yes

I am trying to remove duplicate No and Yes and create column like this

            Col1
    ========================
     No
     No
     Yes
     No
     Yes
     Yes, No

I started with

     kickDuplicates <- c("No","Yes")
     # create a list of vectors of place names
     broken <- strsplit(Table1$Col1, ",")
     # paste each broken vector of place names back together
     # .......kicking out duplicated instances of the chosen names
     Table1$Col1 <- sapply(broken, FUN = function(x)  paste(x[!duplicated(x)  
     | !x %in% kickDuplicates ], collapse = ", "))

But this is not working, i get the same original column with duplicates as before, can anybody tell me where I am going wrong ?

c("\"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\"", 
"\"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"Yes\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\"", 
"\"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\"", 
"\"No\", \"No\"", "\"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\", \"No\"", 
"No")

回答1:


I think this will work as your final line:

Table1$Col1 <- sapply(broken,function(x) paste(unique(x), collapse=','))

Because I am a fan of package functional, here is an equivalent:

sapply(broken, Compose(unique, Curry(paste, collapse=',')))


来源:https://stackoverflow.com/questions/30687966/removing-duplicate-words-in-a-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!