2016-04-30 3 views
1

@someone @ somebodyのようなTwitterのデータをデータフレームのTwitterから引用し、誰がtweetしたのかという情報と新しいデータフレームを作成しようとしています。コメントに言及してデータフレームにデータを埋め込む

例:

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* 

は、あなたたちは、私を助けてくださいことはできますか?

答えて

1

あなたはライブラリstringrを使用して、このような何かを行うことができます。

tweets 

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

ロングフォーマットで出力を得るために、あなたはこのような何かを行うことができます:

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

出力は次のようである

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