簡體   English   中英

如何刪除包含R中某些單詞的數據框中的行?

[英]How to remove rows in a dataframe that contain certain words in R?

我正在嘗試刪除數據框中包含某個單詞或某些單詞序列的行。 例如:

mydf <- as.data.frame(read.xlsx("C:\\data.xlsx, 1, header=T"))
head(df)
#     NO    ARTICLE    
# 1   34    New York Times reports blabla
# 2   42    Financial Times reports blabla
# 3   21    Greenwire reports blabla
# 4    3    New York Times reports blabla
# 5   46    Newswire reports blabla

我想從我的data.frame刪除包含字符串“New York Times”和“Newswire”的data.frame 我嘗試過使用%in%grep不同方法,但我不太清楚如何使用它!

我怎么做?

根據我的評論,使用grepl ,它會在向量中找到指定的字符串時返回邏輯值。 在你的情況下,像:

df[!grepl('New York Times',df$Article),]

應該做的伎倆。

# Sample Data
NO <- c(34, 42, 21, 3)
ARTICLE <- c('New York Times reports blah blah fake news',
             'Financial Times blah blah',
             'Fox News has been very nice to me',
             'Newswire reports blah blah')
df <- data.frame(NO, ARTICLE)

# Create List of Exclusion Phrases
fakenews <- c('New York Times', 'Newswire')

# Exclude
very.nice.to.me <- df[ !grepl(paste(fakenews, collapse="|"), df$ARTICLE),]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM