R删除data.table中的多个文本字符串

时间:2015-07-14 07:30:50

标签: r data.table gsub

我要从vector data.table中删除DT个字词,如下所示。

wordstoremove <- c("Simpson", "Flander", "Nahasapeemapetilon", "Spuckler", "Wiggum")

DT <- structure(list(vid = c("Simpsons", "Flanders", "Nahasapeemapetilons", 
                             "Spucklers", "Wiggums"), wr1 = c("Homer Simpson", "Ned Flanders", 
                                                              "Apu Nahasapeemapetilon", "Cletus Spuckler", "Chief Wiggum"), 
                     wr2 = c("Bart Simpson", "Rod Flanders", "Manjula Nahasapeemapetilon", 
                             "Brandine Spuckler", "Ralph Wiggum"), wr3 = c("Marge Simpson", 
                                                                           "Todd Flanders", "Sanjay Nahasapeemapetilon", NA, "Sarah Wiggum"
                             )), .Names = c("vid", "wr1", "wr2", "wr3"), row.names = c(NA, 
                                                                                       -5L), class = c("data.table", "data.frame"))

DT
                   vid                    wr2                        wr2                       wr3
1:            Simpsons          Homer Simpson               Bart Simpson             Marge Simpson
2:            Flanders           Ned Flanders               Rod Flanders             Todd Flanders
3: Nahasapeemapetilons Apu Nahasapeemapetilon Manjula Nahasapeemapetilon Sanjay Nahasapeemapetilon
4:           Spucklers        Cletus Spuckler          Brandine Spuckler                        NA
5:             Wiggums           Chief Wiggum               Ralph Wiggum              Sarah Wiggum

我知道我可以使用R remove multiple text strings in data frame中的解决方案。

如何使用data.table来最小化数据复制?

1 个答案:

答案 0 :(得分:6)

试试这个:

Chronology