行明智地合并`data.table`中的逗号分隔值

时间:2015-06-24 17:56:36

标签: r data.table paste strsplit

我有data.table DT,如下所示。

DT <- structure(list(ID = c("Bats", "HL", "JL", "Spidey", "Supes", 
"X"), List1 = c("Morrison, Brubaker, Daniel, Loeb", "David, Bryne, Lee", 
"", "Loeb, Lee", "Moore, Siegel, Millar", "Bendis, Whendon"), 
    List2 = c("Rucka, Kane, Morrison", "Lee, Mantlo, Bryne", 
    "Meltzer, Sekowsky, Morrison", "Waid, Yost, Kirby, Lee", 
    "", "Claremont, Whendon, Morrison")), .Names = c("ID", "List1", 
"List2"), row.names = c(NA, -6L), class = c("data.table", "data.frame"
), .internal.selfref = NULL, sorted = "ID")

DT

      ID                            List1                        List2
1:   Bats Morrison, Brubaker, Daniel, Loeb        Rucka, Kane, Morrison
2:     HL                David, Bryne, Lee           Lee, Mantlo, Bryne
3:     JL                                   Meltzer, Sekowsky, Morrison
4: Spidey                        Loeb, Lee       Waid, Yost, Kirby, Lee
5:  Supes            Moore, Siegel, Millar                             
6:      X                  Bendis, Whendon Claremont, Whendon, Morrison

我想在列DT$List1DT$List2中逐行合并两个列表而不重复。

我可以使用apply执行此操作,如下所示。

DT$merged <- apply(DT,1,function(vec){
  paste(unique(strsplit(paste(vec[2],vec[3],sep=", "),", ")[[1]]),collapse=", ")
})

     DT
       ID                            List1                        List2
1:   Bats Morrison, Brubaker, Daniel, Loeb        Rucka, Kane, Morrison
2:     HL                David, Bryne, Lee           Lee, Mantlo, Bryne
3:     JL                                   Meltzer, Sekowsky, Morrison
4: Spidey                        Loeb, Lee       Waid, Yost, Kirby, Lee
5:  Supes            Moore, Siegel, Millar                             
6:      X                  Bendis, Whendon Claremont, Whendon, Morrison
                                          merged
1: Morrison, Brubaker, Daniel, Loeb, Rucka, Kane
2:                     David, Bryne, Lee, Mantlo
3:                 , Meltzer, Sekowsky, Morrison
4:                  Loeb, Lee, Waid, Yost, Kirby
5:                         Moore, Siegel, Millar
6:          Bendis, Whendon, Claremont, Morrison

如何在不使用&#34;,&#34; data.table的情况下有效地获得相同的结果由于空单元格在开始和结束?

4 个答案:

答案 0 :(得分:4)

DT[, merged := toString(unique(c(strsplit(List1, split = ", ")[[1]],
                                 strsplit(List2, split = ", ")[[1]]))), by = ID][]
#       ID                            List1                        List2
#1:   Bats Morrison, Brubaker, Daniel, Loeb        Rucka, Kane, Morrison
#2:     HL                David, Bryne, Lee           Lee, Mantlo, Bryne
#3:     JL                                   Meltzer, Sekowsky, Morrison
#4: Spidey                        Loeb, Lee       Waid, Yost, Kirby, Lee
#5:  Supes            Moore, Siegel, Millar                             
#6:      X                  Bendis, Whendon Claremont, Whendon, Morrison
#                                          merged
#1: Morrison, Brubaker, Daniel, Loeb, Rucka, Kane
#2:                     David, Bryne, Lee, Mantlo
#3:                   Meltzer, Sekowsky, Morrison
#4:                  Loeb, Lee, Waid, Yost, Kirby
#5:                         Moore, Siegel, Millar
#6:          Bendis, Whendon, Claremont, Morrison

如果您的1:nrow(DT)列不是唯一的,请将{by'替换为ID

答案 1 :(得分:3)

尝试

library(data.table)#v1.9.5+
DT[, merged := do.call(paste, c(.SD, sep=", ")), .SDcols= List1:List2
 ][, merged:=unlist(lapply(strsplit(merged, ", "),
          function(x)  toString(unique(x))))]   
#     ID                            List1                        List2
#1:   Bats Morrison, Brubaker, Daniel, Loeb        Rucka, Kane, Morrison
#2:     HL                David, Bryne, Lee           Lee, Mantlo, Bryne
#3:     JL                                   Meltzer, Sekowsky, Morrison
#4: Spidey                        Loeb, Lee       Waid, Yost, Kirby, Lee
#5:  Supes            Moore, Siegel, Millar                             
#6:      X                  Bendis, Whendon Claremont, Whendon, Morrison
#                                          merged
#1: Morrison, Brubaker, Daniel, Loeb, Rucka, Kane
#2:                     David, Bryne, Lee, Mantlo
#3:                 , Meltzer, Sekowsky, Morrison
#4:                  Loeb, Lee, Waid, Yost, Kirby
#5:                         Moore, Siegel, Millar
#6:          Bendis, Whendon, Claremont, Morrison

或者我们可以在regex之后使用do.call(paste删除重复的元素

 DT[, merged := gsub('^,\\s*|(\\b\\S+\\b)(?=.*\\b\\1\\b.*),\\s*|,\\s*$', '',
     do.call(paste, c(.SD, sep=", ")), perl=TRUE), .SDcols = List1:List2]
#   ID                            List1                        List2
#1:   Bats Morrison, Brubaker, Daniel, Loeb        Rucka, Kane, Morrison
#2:     HL                David, Bryne, Lee           Lee, Mantlo, Bryne
#3:     JL                                   Meltzer, Sekowsky, Morrison
#4: Spidey                        Loeb, Lee       Waid, Yost, Kirby, Lee
#5:  Supes            Moore, Siegel, Millar                             
#6:      X                  Bendis, Whendon Claremont, Whendon, Morrison
 #                                         merged
#1: Brubaker, Daniel, Loeb, Rucka, Kane, Morrison
#2:                     David, Lee, Mantlo, Bryne
#3:                   Meltzer, Sekowsky, Morrison
#4:                  Loeb, Waid, Yost, Kirby, Lee
#5:                         Moore, Siegel, Millar
#6:          Bendis, Claremont, Whendon, Morrison

答案 2 :(得分:3)

这可能是另一种方式

DT[, merged:= paste(union(unlist(strsplit(List1, ', ')),
              unlist(strsplit(List2, ', '))), collapse = ', '), by = ID]

#> DT
#       ID                            List1                        List2
#1:   Bats Morrison, Brubaker, Daniel, Loeb        Rucka, Kane, Morrison
#2:     HL                David, Bryne, Lee           Lee, Mantlo, Bryne
#3:     JL                                   Meltzer, Sekowsky, Morrison
#4: Spidey                        Loeb, Lee       Waid, Yost, Kirby, Lee
#5:  Supes            Moore, Siegel, Millar                             
#6:      X                  Bendis, Whendon Claremont, Whendon, Morrison
#                                          merged
#1: Morrison, Brubaker, Daniel, Loeb, Rucka, Kane
#2:                     David, Bryne, Lee, Mantlo
#3:                   Meltzer, Sekowsky, Morrison
#4:                  Loeb, Lee, Waid, Yost, Kirby
#5:                         Moore, Siegel, Millar
#6:          Bendis, Whendon, Claremont, Morrison

答案 3 :(得分:0)

显示另一种方法的简单示例。函数trimComma只是在开头和结尾处删除逗号。它还修剪重复的逗号,可以轻松地与paste

一起使用
trimComma <-function(x)
{
    gsub(",,",",",gsub("^,+|,+$|","",x))
}