R删除数据框中的多个文本字符串 [英] R remove multiple text strings in data frame

查看:246
本文介绍了R删除数据框中的多个文本字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R的新手.我正在寻找从数据框中删除某些单词的方法.由于有多个单词,因此我想将此单词列表定义为字符串,并使用gsub删除.然后转换回数据框并保持相同的结构.

New to R. I am looking to remove certain words from a data frame. Since there are multiple words, I would like to define this list of words as a string, and use gsub to remove. Then convert back to a dataframe and maintain same structure.

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"        

我在想类似的东西

a2 <- apply(a, 1, gsub(wordstoremove, "", a)

,但是在转换回数据帧之前,这显然不起作用.

but clearly this doesnt work, before converting back to a data frame.

推荐答案

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

这篇关于R删除数据框中的多个文本字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆