【问题标题】:Returning Twitter handles per dataframe row返回每个数据帧行的 Twitter 句柄
【发布时间】:2019-02-26 02:47:34
【问题描述】:

给定以下数据框:

df <- as.data.frame(c("Testing @cspenn @test @hi","this is a tweet","this is a tweet with @mention of @twitter"))
names(df)[1] <- "content"

我正在尝试提取每行的单个 Twitter 句柄,而不是一次提取所有句柄。

来自this example,我有这个函数可以将它们全部吐出,但我需要它们保持包含在每一行中。

df$handles <- plyr::ddply(df, c("content"), function(x){
    mention <- unlist(stringr::str_extract_all(x$content, "@\\w+"))
    # some tweets do not contain mentions, making this necessary:
    if (length(mention) > 0){
        return(data.frame(mention = mention))
    } else {
        return(data.frame(mention = NA))    
    }
})

如何只提取每行的句柄,而不是一次提取所有句柄?

【问题讨论】:

    标签: r twitter stringr rtweet


    【解决方案1】:

    你可以这样做。

    xy <- stringr::str_extract_all(df$content, "@\\w+")
    xy <- sapply(xy, FUN = paste, collapse = ", ")  # have all names concatenated
    cbind(df, xy)
    
                                        content                  xy
    1                 Testing @cspenn @test @hi @cspenn, @test, @hi
    2                           this is a tweet                    
    3 this is a tweet with @mention of @twitter  @mention, @twitter
    

    【讨论】:

    • 谢谢 - 最终使用了 tidyverse 解决方案。
    【解决方案2】:
    library(tidyverse)
    
    df %>%
      mutate(mentions = str_extract_all(content, "@\\w+"))
    

    输出:

                                        content            mentions
    1                 Testing @cspenn @test @hi @cspenn, @test, @hi
    2                           this is a tweet                    
    3 this is a tweet with @mention of @twitter  @mention, @twitter
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 2011-12-20
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2017-06-19
      相关资源
      最近更新 更多