从数据框中提取包含关键字的行(RStudio 中的 Twitter 数据)

Extract rows from dataframe that have keywords in them (Twitter data in RStudio)

我在 RStudio 中有一个大型数据框(约 500,000 个观察值),由结构化的 Twitter 数据(即用户名、rewtweet 计数、文本)组成。我想 运行 对推文进行文本分析,以便提取推文文本中包含一个或多个关键字的观察结果。

我已将关键字上传为 keywords_C <- c("climate change","climate","climatechange","global warming","globalwarming")。推文文本存储在我的数据框中标有 text 的列中。

如何创建一个仅包含 text 列中存在一个或多个关键字的观测值的新数据框?或者,我可以删除不存在关键字的观察结果吗?


我的数据框名为 NewCData

dput(droplevels(head(NewCData, 10)))

  structure(list(timestamp = structure(c(1L, 3L, 2L, 6L, 4L, 4L, 
    5L, 8L, 7L, 9L), .Label = c("2015-10-30 21:37:58", "2015-10-30 21:38:02", 
    "2015-10-30 21:38:03", "2015-10-30 21:38:06", "2015-10-30 21:38:07", 
    "2015-10-30 21:38:10", "2015-10-30 21:38:14", "2015-10-30 21:38:32", 
    "2015-10-30 21:39:04"), class = "factor"), id_str = structure(c(1L, 
    3L, 2L, 7L, 4L, 5L, 6L, 9L, 8L, 10L), .Label = c("660209050429186048", 
    "660209067584016384", "660209072768212992", "660209083505504256", 
    "660209086143688704", "660209087628578816", "660209102790914048", 
    "660209119152893952", "660209195162206208", "660209325986549760"
    ), class = "factor"), user.id_str = structure(c(1L, 3L, 8L, 5L, 
    5L, 2L, 4L, 6L, 9L, 7L), .Label = c("277335277", "32380087", 
    "325105950", "33398863", "68956490", "808114195", "87712431", 
    "90280824", "949996219"), class = "factor"), user.followers_count = structure(c(7L, 
    2L, 8L, 4L, 4L, 3L, 6L, 9L, 5L, 1L), .Label = c("10212", "1062", 
    "1389", "15227", "2214", "2851", "38", "4137", "55"), class = "factor"), 
        ideology = structure(c(2L, 4L, 3L, 9L, 9L, 5L, 8L, 6L, 1L, 
        7L), .Label = c("-0.309303177803536", "-0.393703659798908", 
        "-0.795976086971656", "-0.811321629152632", "-0.946143178314071", 
        "-1.16317298915931", "0.353843466445817", "1.09919837237897", 
        "2.29286233202781"), class = "factor"), text = structure(c(2L, 
        9L, 4L, 1L, 3L, 10L, 5L, 7L, 6L, 8L), .Label = c("Better Dead than Red! Bill Gates says that only socialism can save us ", 
        "Expert briefing on  #disarmament #SDGs @NMUN ", 
        "I see red people Bill Gates says that only socialism can save us from climate change ", 
        "RT: Oddly enough, some Republicans think climate change is real: Oddly enough,…  #UniteBlue ", 
        "Ted Cruz: ‘Climate change is not science, it’s religion’  via @glennbeck", 
        "This is an amusing headline: \"Bill Gates says that only socialism can save us from climate change\"", 
        "Unusual Weather Kills Gulf of Maine Cod : Discovery News #globalwarming  ", 
        "What do the remaining Republican candidates have to say about climate change? #FixGov", 
        "Who Uses #NASA Earth Science Data? He looks at impact of #aerosols on #climate #weather!", 
        "Why go for ecosystem basses conservation! #ClimateChange #Raajje #Maldives"
        ), class = "factor")), .Names = c("timestamp", "id_str", 
    "user.id_str", "user.followers_count", "ideology", "text"), row.names = c(NA, 
    10L), class = "data.frame")

您可以使用

new_df <- NewCData[with(NewCData, grepl(paste0("\b(?:",paste(keywords_C, collapse="|"),")\b"), text)),]

R demo online

这里的重点是将关键字组合成一个模式,如

\b(?:climate change|climate|climatechange|global warming|globalwarming)\b

它会将单词作为整个单词进行匹配,如果 text 列中有匹配项,则返回该行,否则,该行将被丢弃。