删除连续的重复单词

时间:2015-06-06 21:59:40

标签: r duplicates strsplit

我在表格中有一列如下

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

我正在尝试删除重复的No和Yes并创建像这样的列

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

我从

开始
     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 = ", "))

但这不起作用,我和以前一样重复原始专栏,有人可以告诉我哪里出错了吗?

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 个答案:

答案 0 :(得分:1)

我认为这将作为你的最后一行:

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

因为我是包functional的粉丝,所以这是等效的:

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