我正试图从数据框twitter上提及像@someone @somebody这样的Twitter数据,并创建一个新数据框,其中包含推文和他们提到的人的信息。
示例:
BOOL
导致此数据框:
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*
你能帮我吗?
答案 0 :(得分:1)
您可以使用库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