R.的新功能我希望从数据框中删除某些单词.由于有多个单词,我想将这个单词列表定义为字符串,并使用gsub删除.然后转换回数据帧并保持相同的结构.
wordstoremove <- c("ai", "computing", "ulitzer", "ibm", "privacy", "cognitive")
a
id text time username
1 "ai and x" 10 "me"
2 "and computing" 5 "you"
3 "nothing" 15 "everyone"
4 "ibm privacy" 0 "know"
Run Code Online (Sandbox Code Playgroud)
我想的是:
a2 <- apply(a, 1, gsub(wordstoremove, "", a)
Run Code Online (Sandbox Code Playgroud)
但在转换回数据框之前,这显然不起作用.
wordstoremove <- c("ai", "computing", "ulitzer", "ibm", "privacy", "cognitive")
(dat <- read.table(header = TRUE, text = 'id text time username
1 "ai and x" 10 "me"
2 "and computing" 5 "you"
3 "nothing" 15 "everyone"
4 "ibm privacy" 0 "know"'))
# id text time username
# 1 1 ai and x 10 me
# 2 2 and computing 5 you
# 3 3 nothing 15 everyone
# 4 4 ibm privacy 0 know
(dat1 <- as.data.frame(sapply(dat, function(x)
gsub(paste(wordstoremove, collapse = '|'), '', x))))
# id text time username
# 1 1 and x 10 me
# 2 2 and 5 you
# 3 3 nothing 15 everyone
# 4 4 0 know
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6014 次 |
| 最近记录: |