gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:32:27 24 4
gpt4 key购买 nike

我有一个 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 来最小化数据复制?

最佳答案

试试这个:

library(data.table)
foo <- function(x) gsub(paste0(wordstoremove, collapse="s?|"), "", x)
DT[, names(DT)[-1] := lapply(.SD, foo), .SDcols = names(DT)[-1]]
DT
# vid wr1 wr2 wr3
# 1: Simpsons Homer Bart Marge
# 2: Flanders Ned Rod Todd
# 3: Nahasapeemapetilons Apu Manjula Sanjay
# 4: Spucklers Cletus Brandine NA
# 5: Wiggums Chief Ralph Sarah

关于R删除data.table中的多个文本字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31400374/

24 4 0