筛选推文

用 R 分析社交媒体数据

Vivek Vijayaraghavan

Data Science Coach

课程概览

  • 基于推文组成部分筛选
    • 提取原创推文
    • 推文语言
    • 按最低转推和点赞数筛选热门推文
用 R 分析社交媒体数据

筛选原创推文

  • 原创推文是用户首次发布的内容
  • 不是转推、引用或回复
  • 原创推文可避免内容重复
  • 有助于保持用户参与度
用 R 分析社交媒体数据

筛选原创推文

  • 使用 -filter 提取原创推文
  • -filter:retweets 排除所有转推
  • -filter:quote 排除引用推文
  • -filter:replies 排除回复类推文
用 R 分析社交媒体数据

无筛选提取推文

  • 在不加筛选的情况下提取"digital marketing"的推文
# 提取 100 条关于"digital marketing"的推文
tweets_all <- search_tweets("digital marketing", n = 100)
用 R 分析社交媒体数据

无筛选提取推文

  • 检查列 reply_to_screen_name、is_quote、is_retweet 的计数
# 检查回复数量
library(plyr)
count(tweets_all$reply_to_screen_name)
x               freq
<fct>          <int>
blairaasmith      2            
javiergosende     1            
juanburgos        1            
WhutTheHale       2            
NA               94
用 R 分析社交媒体数据

无筛选提取推文

# 检查引用数量
count(tweets_all$is_quote)
x        freq
<lgl>    <int>
FALSE     98            
TRUE       2
用 R 分析社交媒体数据

无筛选提取推文

# 检查转推数量
count(tweets_all$is_retweet)
x         freq
<lgl>    <int>
FALSE      61            
TRUE       39
用 R 分析社交媒体数据

排除转推、引用和回复

  • 提取"digital marketing"的推文,并应用 -filter
# 应用 '-filter'
tweets_org <- search_tweets("digital marketing 
                            -filter:retweets 
                            -filter:quote 
                            -filter:replies", 
                            n = 100)
用 R 分析社交媒体数据

排除转推、引用和回复

  • 检查输出以确认已排除回复、引用和转推
# 检查回复数量
library(plyr)
count(tweets_org$reply_to_screen_name)
x         freq
<lgl>    <int>
NA         100
用 R 分析社交媒体数据

排除转推、引用和回复

# 检查引用数量
library(plyr)
count(tweets_org$is_quote)

x         freq
<lgl>    <int>
FALSE     100
# 检查转推数量
library(plyr)
count(tweets_org$is_retweet)
x         freq
<lgl>    <int>
FALSE     100
用 R 分析社交媒体数据

按语言筛选推文

  • lang 按语言筛选推文
  • 匹配特定语言的推文

部分语言的 Twitter 语言代码

用 R 分析社交媒体数据

按语言筛选推文

# 筛选并提取西班牙语推文
tweets_lang <- search_tweets("brand marketing", lang = "es")
用 R 分析社交媒体数据

按语言筛选推文

View(tweets_lang)

提取的西班牙语推文

用 R 分析社交媒体数据

按语言筛选推文

head(tweets_lang$lang)
[1] "es" "es" "es" "es" "es" "es"
用 R 分析社交媒体数据

按转推和点赞数筛选

  • min_faves: 按最低点赞数筛选
  • min_retweets: 按最低转推数筛选
  • 使用 AND 同时满足两项条件
用 R 分析社交媒体数据

按转推和点赞数筛选

# 提取至少 100 点赞且 100 转推的推文 
tweets_pop <- search_tweets("bitcoin min_faves:100 AND
                            min_retweets:100")
用 R 分析社交媒体数据

按转推和点赞数筛选

# 创建数据框以查看转推和点赞数
counts <- tweets_pop[c("retweet_count", "favorite_count")]
head(counts)
retweet_count    favorite_count
    <int>              <int>
1    162                833
2    141                894    
3    164                1128
4    395                1346    
5    475                2271
6    270                1654
用 R 分析社交媒体数据

按转推和点赞数筛选

# 查看推文
head(tweets_pop$text)
text    
<chr>
1    As we continue to build the Bakkt Bitcoin Futures contract, we reached a
2    BREAKING: The United States is considering entering into a "currency pact"
3    REMINDER: The Bitcoin ETF will eventually get approved.\n\nNot a question
4    [New Post] Bitcoin is becoming much more important in Hong Kong and India.
5    Reports are surfacing that some Hong Kong ATMs have run out of cash as
6    Bitcoin is the most transparent currency ever created.
用 R 分析社交媒体数据

Ayo berlatih!

用 R 分析社交媒体数据

Preparing Video For Download...