R:如何删除语料库中除特定单词以外的单词

问题描述 投票:0回答:1

在语料库“ tkn_pb”中,我想删除所有单词,除了我选择的某些关键字(例如,“ attack”和“ gunman”)。可以这样做吗?

enter image description here

r text-mining corpus
1个回答
0
投票

您可以使用whichgrepl作为tkn_pb的子集:

数据:

tkn_pb <- c("word", "another","a", "new", "word token", "one", "more", "and", "another one")

删除除“ a”和“ and”以外的所有单词:

tkn_pb[which(grepl("\\b(a|and)\\b", tkn_pb))]
[1] "a"   "and"
© www.soinside.com 2019 - 2024. All rights reserved.