1

我正在尝试从数据框 twitter 中获得像 @someone @somebody 这样的 Twitter 数据的提及,并创建一个新的数据框,其中包含谁发了推文以及他们提到了哪些人的信息。

例子:

tweets <- data.frame(user=c("people","person","ghost"),text = c("Hey, check this out 
@somebody @someone","love this @john","amazing"))

结果在这个数据框上:

**user     text**

*people   Hey, check this out @somebody @someone*

*person   love this @john*

*ghost    amazing*

期望的结果是:

**id      mention**

*people  @somebody*

*people  @someone*

*person  john*

*ghost*

你们能帮帮我吗?

4

1 回答 1

1

你可以通过使用 library 来做这样的事情stringr

library(stringr)
tweets$mention <- str_extract_all(tweets$text, '\\@\\S+')

输出如下:

tweets

    user                                     text             mention
1 people Hey, check this out \n@somebody @someone @somebody, @someone
2 person                          love this @john               @john
3  ghost                                  amazing                    

要获得长格式的输出,您可以执行以下操作:

library(dplyr)
library(tidyr)
tweets <- rbind(filter(tweets, !grepl('\\@', mention)), unnest(tweets))
tweets <- tweets[, -2]

输出如下:

    user   mention
1  ghost          
2 people @somebody
3 people  @someone
4 person     @john
于 2016-04-30T04:31:12.927 回答