How to get the count on likes and comments for a list of twitter handle

1.9k Views Asked by At

I am working in R and trying to extract the number of total likes and comments for a list of twitter handles. I am completely clueless, any help will be appreciated. I have set up the API and installed the necessary libraries.

Any leads will be appreciated, thank you

install.packages("twitteR")
library(twitteR)
install.packages("rtweet")
library(rtweet)
install.packages("tidytext")
library(tidytext)
install.packages("httr")
library(httr)
install.packages("RCurl")
library(RCurl)
install.packages("plyr")
library(plyr)
install.packages("RJSONIO")
library(RJSONIO)
install.packages("stringr")
library(stringr)
install.packages("ROAuth")
library(ROAuth)
#Setting up twitter API

consumer_key <- "key"
consumer_secret <- "key"
access_token <- "key"
access_secret <- "key"
options(httr_oauth_cache=T) 
setup_twitter_oauth(consumer_key,
                consumer_secret,
                access_token,
                access_secret)

#reading csv and extracting the followers and tweets count


users <- read.csv("Twitter.csv", skip = 1)
users1 <- lookupUsers(users[1:50,1]) 
1

There are 1 best solutions below

9
JohnCoene On

In R, using rtweet, below is the information you will get about a user

library(rtweet)

x <- rtweet::lookup_users("jdatap")
names(x)
 [1] "user_id"                 "status_id"               "created_at"             
 [4] "screen_name"             "text"                    "source"                 
 [7] "display_text_width"      "reply_to_status_id"      "reply_to_user_id"       
[10] "reply_to_screen_name"    "is_quote"                "is_retweet"             
[13] "favorite_count"          "retweet_count"           "hashtags"               
[16] "symbols"                 "urls_url"                "urls_t.co"              
[19] "urls_expanded_url"       "media_url"               "media_t.co"             
[22] "media_expanded_url"      "media_type"              "ext_media_url"          
[25] "ext_media_t.co"          "ext_media_expanded_url"  "ext_media_type"         
[28] "mentions_user_id"        "mentions_screen_name"    "lang"                   
[31] "quoted_status_id"        "quoted_text"             "quoted_created_at"      
[34] "quoted_source"           "quoted_favorite_count"   "quoted_retweet_count"   
[37] "quoted_user_id"          "quoted_screen_name"      "quoted_name"            
[40] "quoted_followers_count"  "quoted_friends_count"    "quoted_statuses_count"  
[43] "quoted_location"         "quoted_description"      "quoted_verified"        
[46] "retweet_status_id"       "retweet_text"            "retweet_created_at"     
[49] "retweet_source"          "retweet_favorite_count"  "retweet_retweet_count"  
[52] "retweet_user_id"         "retweet_screen_name"     "retweet_name"           
[55] "retweet_followers_count" "retweet_friends_count"   "retweet_statuses_count" 
[58] "retweet_location"        "retweet_description"     "retweet_verified"       
[61] "place_url"               "place_name"              "place_full_name"        
[64] "place_type"              "country"                 "country_code"           
[67] "geo_coords"              "coords_coords"           "bbox_coords"            
[70] "status_url"              "name"                    "location"               
[73] "description"             "url"                     "protected"              
[76] "followers_count"         "friends_count"           "listed_count"           
[79] "statuses_count"          "favourites_count"        "account_created_at"     
[82] "verified"                "profile_url"             "profile_expanded_url"   
[85] "account_lang"            "profile_banner_url"      "profile_background_url" 
[88] "profile_image_url"

EDIT

Based on your updated code. Below is a reproducible example.

users <- data.frame(
    name = c("jdatap", "dataandme"),
    stringsAsFactors = FALSE
)

rtweet::lookup_users only works on objects of class character

users <- data.frame(
    name = c("jdatap", "dataandme")
)

class(users$name)
[1] "factor"

rtweet::lookup_users(users = users$name)
data frame with 0 columns and 0 rows

Whereas, with a character vector all goes well.

users <- data.frame(
    name = c("jdatap", "dataandme"),
    stringsAsFactors = FALSE
)

rtweet::lookup_users only works on objects of class character

users <- data.frame(
    name = c("jdatap", "dataandme"),
    stringsAsFactors = FALSE
)

class(users$name)
[1] "character"

# works just not showing huge output here
users <- rtweet::lookup_users(users = users$name)

So when you read the csv with read.csv set stringsAsFactors = FALSE like so.

users <- read.csv("Twitter.csv", skip = 1, stringsAsFactors = FALSE)

Note the above is based on the rtweet package, I strongly suggest you switch from using twitteR, as stated on the twitteR github page.

This is the start of a relatively leisurely deprecation period for twitteR, in favor of using rtweet. Please start looking to switch over to that package. If you have any questions contact myself or @mkearney