$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Redditで遊ぼう #TokyoR 106
Search
bob3bob3
June 10, 2023
Technology
0
760
Redditで遊ぼう #TokyoR 106
Redditの Web API をRから操作していろいろなデータを抽出して遊ぶ。
bob3bob3
June 10, 2023
Tweet
Share
More Decks by bob3bob3
See All by bob3bob3
RとLLMで自然言語処理
bob3bob3
3
750
RでPSM分析
bob3bob3
1
340
Rでコンジョイント分析 2024年版
bob3bob3
0
1.9k
『改訂新版前処理大全』の話と Apache Parquet の話 #TokyoR
bob3bob3
0
1.3k
R言語の環境構築と基礎 Tokyo.R 112
bob3bob3
0
620
『データ可視化学入門』をPythonからRに翻訳した話(増強版)
bob3bob3
0
550
『データ可視化学入門』を PythonからRに翻訳した話
bob3bob3
1
620
qeMLパッケージの紹介
bob3bob3
0
2.3k
「国と音楽」 ~spotifyrを用いて~ #muana
bob3bob3
2
610
Other Decks in Technology
See All in Technology
ChatGPTで論⽂は読めるのか
spatial_ai_network
11
29k
AWS CLIの新しい認証情報設定方法aws loginコマンドの実態
wkm2
7
760
多様なデジタルアイデンティティを攻撃からどうやって守るのか / 20251212
ayokura
0
490
AI 駆動開発勉強会 フロントエンド支部 #1 w/あずもば
1ftseabass
PRO
0
400
子育てで想像してなかった「見えないダメージ」 / Unforeseen "hidden burdens" of raising children.
pauli
2
300
マイクロサービスへの5年間 ぶっちゃけ何をしてどうなったか
joker1007
17
6.7k
ペアーズにおけるAIエージェント 基盤とText to SQLツールの紹介
hisamouna
0
410
AIプラットフォームにおけるMLflowの利用について
lycorptech_jp
PRO
1
170
Power of Kiro : あなたの㌔はパワステ搭載ですか?
r3_yamauchi
PRO
0
180
JEDAI認定プログラム JEDAI Order 2026 エントリーのご案内 / JEDAI Order 2026 Entry
databricksjapan
0
140
AlmaLinux + KVM + Cockpit で始めるお手軽仮想化基盤 ~ 開発環境などでの利用を想定して ~
koedoyoshida
0
120
Haskell を武器にして挑む競技プログラミング ─ 操作的思考から意味モデル思考へ
naoya
7
1.6k
Featured
See All Featured
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
63
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
11
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
132
19k
Ruling the World: When Life Gets Gamed
codingconduct
0
91
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.8k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
170
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.3k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
20
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
390
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
60
37k
Docker and Python
trallard
47
3.7k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
60
Transcript
Redditで遊ぼう Tokyo.R #106 2023/06/10 @bob3bob3
Redditとは Wikipediaによると…… • Reddit(レディット)はアメリカ合衆国の掲示 板型ソーシャルニュースサイト。 • 主に英語圏のユーザーを対象とする。 • ニュース記事、画像のリンクやテキストを投 稿し、コメントをつけることが可能。
• 2021年1月時点の月間利用者数は 4億3000 万人。欧米ではTwitterユーザー数並び利用 時間を超える。
ブッコ抜く! RedditExtractoRパッケージを使うとRedditの公式APIからいろいろなデータが取得でき ます。 自然言語処理やネットワーク分析の勉強など、いろいろ遊べるリアルなデータが手に入 る。 Twitterのデータが取れなくなってガッカリしている方々もぜひ。 主な関数は以下。 • find_subreddits(): キーワードでsubreddit(板)を検索する。
• find_thread_urls(): 特定のsubredditからthread(スレ)のURLを抽出する。 • get_thread_content(): threadの詳細情報を抽出する。 • get_user_content(): ユーザーの詳細情報を抽出する。
Rのスレを抽出する find_thread_urls() library(conflicted) library(tidyverse) library(RedditExtractoR) thread_URLs <- find_thread_urls( sort_by = "top",
subreddit = "rstats", period = "month" ) |> drop_na() |> as_tibble() RのsubredditのURLは https://www.reddit.com/r/rstats/ 。 • find_thread_urls()を使って指定した板 (subreddit)から各スレのURLを抽出します。 • URLの赤文字の部分でsubredditを指定す る。 • ソート方法はhot、new、top、rising。 • periodは期間の指定でhour、day、week、 month、year、all。 • キーワードの設定もできます。 • なぜか空行が1行入るので最後にdrop_na() する。 • 最後は個人的な好みで tibbleにしてます。 • 取得の上限は1,000件です。
Rのスレを抽出する find_thread_urls()
各スレの詳細情報を取得する get_thread_contets() 先ほど取得したスレッドのリストの URLの情報を 使って、各スレの詳細情報を取得する。 1件あたり2秒ぐらいかかるので 100件ぐらいだと3 分ちょっとかかる。 取得した情報にはスレそのものの詳細情報と、ス レについたコメントの情報が含まれるのでそれぞ
れを取り出しておく。 thread_contents <- thread_URLs |> pull(url) |> get_thread_content() # 各スレの詳細情報 threads_info <- thread_contents |> pluck("threads") # 各スレについたコメントの情報 comments_info <- thread_contents |> pluck("comments")
スレッドの詳細情報
スレのコメント
スコアの高いスレ #scoreの高いスレ threads_top10 <- threads_info |> slice_max(score, n=10) |> select(author,
title, score, url) "マイクロソフトはCRAN Time Machineの保守を終了し ました"
スコアの高いコメント #scoreの高いコメント comments_top10 <- comments_info |> slice_max(score, n=10) |> select(author,
score, comment, url) "人々がつまづくのを見るので、これについての重要な注 意:!!はtidyverseの関数呼び出しで*だけ*動作します。そ れは*全体的なR言語の機能ではありません *。"
ネットワークにしてみる 「誰が立てたスレッドに誰がコメントしたか?」という 視点でネットワークにしてみる。 • URLをキーにスレの情報とコメントの情報を join。 • 削除されたアカウントは取り除く • スレを立てた場合とコメントした場合でノード
を分けたいので、スレを立てた場合はアカウ ント名の後に「_poster」を付ける。 edges_raw <- threads_info |> select(url, author) |> left_join( comments_info |> select(url, author), by = "url", suffix = c(".thread", ".comment"), ) |> select(!url) |> drop_na() |> dplyr::filter( author.thread != "[deleted]" | author.comment != "[deleted]" ) |> mutate( author.thread = paste0( author.thread, "_poster" ))
ノードのリストを作る ノードのリストを作る • せっかくなので各ノードの次数も igraphで計 算しておく。 # ノードごとの次数を計算 library(igraph) dgr
<- edges_raw |> graph_from_data_frame() |> degree() # ノードのリストを作る nodes <- edges_raw |> select(author.thread) |> distinct() |> mutate(type = "poster") |> rename(id = author.thread) |> bind_rows( bi_graph |> select(author.comment) |> distinct() |> mutate(type = "commenter") |> rename(id = author.comment) ) |> left_join( tibble( id = dgr |> names(), dgr = dgr ), by = "id")
エッジのリストを作る スレ主とコメント主でグループ化し件数をカウント。 後でCytoscapeに読み込ませたいので、列名をス レ主はtarget、コメント主はsourceに変更。 # あとでCytoscapeに読ませる関係で # 列名をsourceとtargetに変更 edges <-
edges_raw |> summarise( freq = n(), .by = c(author.thread, author.comment) ) |> rename( source = author.comment, target = author.thread )
Cytoscapeで描画 ネットワーク図の描画は Cytoscapeが圧倒的に優 れている。 • Rcy3パッケージでRからCytoscapeを操作で きる。Rcy3はCRANではなくBioconductorか らインストールする。 • 事前にCytoscapeをインストールし、
Cytoscape内でCyRESTをインストールして おく。 • また、createNetworkFromDataFrames()を 実行する前にCytoscapeを立ち上げておく。 # install.packages("BiocManager") # BiocManager::install("RCy3") library(RCy3) createNetworkFromDataFrames( nodes, edges, title="rstats network", collection="Reddit Network" )
Cytoscapeで描画 見栄えはCytoscapeでいじってます。 赤がスレ主。
Enjoy!