Rで学ぶソーシャルメディアデータ分析
Sowmya Vivek
Data Science Coach
screen_name はユーザーのTwitterハンドルを保持
rtweet ライブラリでTwitter JSONをデータフレーム化
# search_tweets() で「#brexit」のツイートを取得
tweets_df <- search_tweets("#brexit")
# 列名を確認
names(tweets_df)

screen_name: ユーザー関心の把握followers_count: 影響力の比較retweet_count と text: 人気ツイートの特定screen_name はTwitterハンドル# search_tweets() で「#Arsenal」のツイートを取得
twts_arsnl <- search_tweets("#Arsenal", n = 18000)
# ユーザーごとの投稿数テーブルを作成
sc_name <- table(twts_arsnl$screen_name)
head(sc_name)
_____today_____ ___JJ23 ___SAbI__ __ambell __Amzo__ __bobbysingh
1 2 3 1 1 1
# 投稿数の降順でソート
sc_name_sort <- sort(sc_name, decreasing = TRUE)
# 上位6ユーザーと頻度を表示
head(sc_name_sort)
_whatthesport footy90com Official_ATG1 TheShortFuse RubellM ArsenalZone_Ind
176 90 88 53 48 43
# lookup_users() でユーザーデータを取得
tvseries <- lookup_users("GameOfThrones", "fleabag", "BreakingBad")
# screen_name と followers_count 列のデータフレームを作成
user_df <- tvseries[,c("screen_name","followers_count")]
# フォロワー数を表示して比較
user_df
screen_name followers_count
<chr> <int>
GameOfThrones 8597188
fleabag 58727
BreakingBad 1240349
retweet_count はリツイート数を保持# ツイート本文とリツイート数のデータフレームを作成
rtwt <- tweets_arsenal[,c("text", "retweet_count")]
# リツイート数の降順でソート
library(dplyr)
rtwt_sort <- arrange(rtwt, desc(retweet_count))
# 重複する本文を除外
rtwt_unique <- unique(rtwt_sort, by = "text")
# 最多リツイートのユニーク投稿 上位6件を表示
head(rtwt_unique)
retweet_count text
<int> <chr>
5606 Once a Gunner, Always a Gunner. We are proud of you @alexanderiwob
3764 Emirates on Fire 🔥🔥🔥🔥 Never give up Gunners💪🏽💪🏽💪🏽 #Arsenal #CO
2798 That mood tonight ⚡️⚡️⚡️ 3️⃣ POINTS 🔴⚪️ #Arsenal #Gunners #COYG h
2741 #Arsenal fan: "I reckon we'll win the League this season." @Robbie
1687 Auba 😭😭😍😍 This is what I call happiness #aubameyang #arsenal
1166 When sky sports introduced the new Monday night football! The Sha
Rで学ぶソーシャルメディアデータ分析