2017-01-15 10 views
0

エッジに関連するグラフの追加属性にアクセスする方法が不思議です。ここに沿うするには、最小限の例です:R(igraph)のサブグラフテキスト解析

library("igraph") 
library("SocialMediaLab") 

myapikey ='' 
myapisecret ='' 
myaccesstoken = '' 
myaccesstokensecret = '' 

tweets <- Authenticate("twitter", 
         apiKey = myapikey, 
         apiSecret = myapisecret, 
         accessToken = myaccesstoken, 
         accessTokenSecret = myaccesstokensecret) %>% 
Collect(searchTerm="#trump", numTweets = 100,writeToFile=FALSE,verbose=TRUE) 
g_twitter_actor <- tweets %>% Create("Actor", writeToFile=FALSE) 
c <- igraph::components(g_twitter_actor, mode = 'weak') 
subCluster <- induced.subgraph(g_twitter_actor, V(g_twitter_actor)[which(c$membership == which.max(c$csize))]) 

初期ツイートは次の列が含まれてい

colnames(tweets) 
[1] "text"   "favorited"  "favoriteCount" "replyToSN"  "created_at"  "truncated"  "replyToSID"  "id"    
[9] "replyToUID"  "statusSource" "screen_name"  "retweetCount" "isRetweet"  "retweeted"  "longitude"  "latitude"  
[17] "from_user"  "reply_to"  "users_mentioned" "retweet_from" "hashtags_used" 

は、どのように私は、テキスト分析を実行するために、部分グラフのテキストプロパティにアクセスすることができますか? E(subCluster)$textが動作しない

答えて

1

E(subCluster)$texttweets$textの値が作成されたときにグラフに追加されないため、機能しません。だからあなたは手動でそれをしなければならない。それは少し痛みですが、実りあります。 tweetsデータフレームをサブセット化し、ユーザー名に基づいて一致させる必要があります。

最初に、エッジの種類が特定の順序になっていることに注意してください:リツイート、言及、返信。特定のユーザーからの同じテキストは、これらの3つすべてに適用できます。だから私は、テキストを連続して追加することが理にかなっていると思います。 dplryreshape2を使用して

> unique(E(g_twitter_actor)$edgeType) 
[1] "Retweet" "Mention" "Reply" 

は、これが容易になります。

library(reshape2); library(dplyr) 
#Make data frame for retweets, mentions, replies 
rts <- tweets %>% filter(!is.na(retweet_from)) 
ms <- tweets %>% filter(users_mentioned!="character(0)") 
rpls <- tweets %>% filter(!is.na(reply_to)) 

users_mentionedには個人のリストが含まれるため、リストから除外する必要があります。しかし、私たちは言及したユーザーを、そのユーザーを言及したユーザーに関連づけたいと考えています。

#Name each element in the users_mentioned list after the user who mentioned 
names(ms$users_mentioned) <- ms$screen_name 
ms <- melt(ms$users_mentioned) #melting creates a data frame for each user and the users they mention 

#Add the text 
ms$text <- tweets[match(ms$L1,tweets$screen_name),1] 

エッジタイプを一致させることで、エッジ属性としてネットワークにこれらをそれぞれ追加します。

E(g_twitter_actor)$text[E(g_twitter_actor)$edgeType %in% "Retweet"] <- rts$text 
E(g_twitter_actor)$text[E(g_twitter_actor)$edgeType %in% "Mention"] <- ms$text 
E(g_twitter_actor)$text[E(g_twitter_actor)$edgeType %in% "Reply"] <- rpls$text 

ここで、テキストのエッジ値をサブセット化して取得できます。

subCluster <- induced.subgraph(g_twitter_actor, 
          V(g_twitter_actor)[which(c$membership == which.max(c$csize))]) 
+0

おかげで多くは、あなたが私もhttp://stackoverflow.com/questions/41664769/igraph-get-ids-of-connected-componentsを解決する方法を知っていますか? –