Import Packages

To first search twitter users we will load the rtweet library. A library is a collection of functions that someone else has provided so that we don’t have to do the work of explaining to the computer what we want to do. We can just call the already created functions. We use functions from other packages here as well such as the tidyverse

library(rtweet)
library(kableExtra)
library(tidyverse)

Search Users

The rtweet package includes a function called search_users() once we have loaded the library we can simply type search_users().

Search Users Arguments

Arguments are information we pass to a function so that it knows what kind of information we are interested in returning. For example with search_users() we might want to give it a term to search users for. Here are the arguments we can pass to search_users()

search_users(q, n = 100, parse = TRUE, token = NULL, verbose = TRUE)
  • q [string] - What search term you want to look for when looking for users. This seems to be related to screen_name, user_name and description
  • n [integer] - the number of users you want to return. If you don’t specify the default will be to return 100 the most you can return is 1000
  • parse [boolean] - Whether to return the twitter response object (JSON) or a data.frame the default is TRUE and that’s the best thing to leave it as. It will make working with the data easier.
  • token [Token] - If you have a developer token created by the create_token() function you can pass it with this argument. It should not be needed for asearch_users() call
  • verbose [boolean] - whether to output debug messages or not. This will print in the console what the function is working on and any errors it encounters.

Search User Returns

The search_users function returns a list of user objects. User object contain a lot of data on users (90 variables!!) some are not very useful for our research while others can be quite useful. We will look at some useful ones below.

  • screen_name [string] - This is the @ name for the account it is permanent
  • name [string] - This is the user’s display name and can be changed
  • description [string] - The short text blurb on the user’s account
  • location [string] - The user-defined location. This could be real place or not.
  • created_at [datetime] - When the account was created
  • followers_count [numeric] - number of followers account has
  • friends_count [numeric] - number of accounts the user is following
  • listed_count [numeric] - number of public list user is a member of
  • favourites_count [numeric] - number of tweet user has liked in their lifetime
  • statuses_count [numeric] - The number of tweets issued by the user

Here is an example output for what search_users returns. This is a very Long table (90 variables!!) but you can scroll sideways to see it all.

data <- search_users(q = "chico", n = 10, verbose = FALSE)
kable(data, "html") %>% 
  kable_styling("striped") %>% 
  scroll_box(width = "100%")
user_id status_id created_at screen_name text source display_text_width reply_to_status_id reply_to_user_id reply_to_screen_name is_quote is_retweet favorite_count retweet_count quote_count reply_count hashtags symbols urls_url urls_t.co urls_expanded_url media_url media_t.co media_expanded_url media_type ext_media_url ext_media_t.co ext_media_expanded_url ext_media_type mentions_user_id mentions_screen_name lang quoted_status_id quoted_text quoted_created_at quoted_source quoted_favorite_count quoted_retweet_count quoted_user_id quoted_screen_name quoted_name quoted_followers_count quoted_friends_count quoted_statuses_count quoted_location quoted_description quoted_verified retweet_status_id retweet_text retweet_created_at retweet_source retweet_favorite_count retweet_retweet_count retweet_user_id retweet_screen_name retweet_name retweet_followers_count retweet_friends_count retweet_statuses_count retweet_location retweet_description retweet_verified place_url place_name place_full_name place_type country country_code geo_coords coords_coords bbox_coords status_url name location description url protected followers_count friends_count listed_count statuses_count favourites_count account_created_at verified profile_url profile_expanded_url account_lang profile_banner_url profile_background_url profile_image_url
17068492 1243686001702289408 1585352824 ChicoState Made it through the first week of virtual classes! Take some well-deserved rest this weekend—maybe spend some (socially distanced) time in nature to relax. (📸 by @jasonhalley_csu) https://t.co/0exNvZSEXJ Sprout Social 180 NA NA NA FALSE FALSE 170 28 NA NA NA NA NA NA NA http://pbs.twimg.com/media/EUJ1zqWXkAIsLDv.jpg https://t.co/0exNvZSEXJ https://twitter.com/ChicoState/status/1243686001702289408/photo/1 photo c(“http://pbs.twimg.com/media/EUJ1zqWXkAIsLDv.jpg”, “http://pbs.twimg.com/media/EUJ10DgWkAA1Y8W.jpg”, “http://pbs.twimg.com/media/EUJ10aeWsAAH70M.jpg”, “http://pbs.twimg.com/media/EUJ10rzXkAE7kfx.jpg”) c(“https://t.co/0exNvZSEXJ”, “https://t.co/0exNvZSEXJ”, “https://t.co/0exNvZSEXJ”, “https://t.co/0exNvZSEXJ”) c(“https://twitter.com/ChicoState/status/1243686001702289408/photo/1”, “https://twitter.com/ChicoState/status/1243686001702289408/photo/1”, “https://twitter.com/ChicoState/status/1243686001702289408/photo/1”, “https://twitter.com/ChicoState/status/1243686001702289408/photo/1”) NA 403935444 JasonHalley_CSU en NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA c(NA, NA) c(NA, NA) c(NA, NA, NA, NA, NA, NA, NA, NA) https://twitter.com/NA/status/1243686001702289408 Chico State Chico, CA The official Twitter account of California State University, Chico — home of the Wildcats! https://t.co/OdysBYADEw FALSE 21519 259 303 13661 13346 1225384171 TRUE https://t.co/OdysBYADEw http://www.csuchico.edu NA https://pbs.twimg.com/profile_banners/17068492/1583531164 http://abs.twimg.com/images/themes/theme7/bg.gif http://pbs.twimg.com/profile_images/1236043257563340800/1MvpXXdn_normal.jpg
41171271 1244044718578774016 1585438348 ChicoER Phil Bryant All-Star Game rosters finalized prior to postponement of annual Northern Section basketball showcase https://t.co/50ehM1cJ68 SocialFlow 136 NA NA NA FALSE FALSE 0 0 NA NA NA NA trib.al/5DJu8LT https://t.co/50ehM1cJ68 https://trib.al/5DJu8LT NA NA NA NA NA NA NA NA NA NA en NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA c(NA, NA) c(NA, NA) c(NA, NA, NA, NA, NA, NA, NA, NA) https://twitter.com/NA/status/1244044718578774016 Chico Enterprise-Record Chico, Calif. Top local news about Chico and Northern California. Follow us on Facebook, Instagram and YouTube. http://t.co/31qF8gjQRU FALSE 12619 266 338 55765 323 1242756613 TRUE http://t.co/31qF8gjQRU http://chicoer.com NA https://pbs.twimg.com/profile_banners/41171271/1487104025 http://abs.twimg.com/images/themes/theme1/bg.png http://pbs.twimg.com/profile_images/994354408111538176/kFrX8pZj_normal.jpg
70509424 1243862550518915074 1585394916 Chicos An #OOTD for all your virtual plans. From museum visits to Broadway plays, we’ve created a list of 24 activities to get you through the weekend. Click here to read more on our blog, Inside Chic: https://t.co/FrUHPyA88i https://t.co/V8e54eK8Te Sprinklr 218 NA NA NA FALSE FALSE 7 1 NA NA OOTD NA spr.ly/60101vUPo https://t.co/FrUHPyA88i http://spr.ly/60101vUPo http://pbs.twimg.com/media/EUMWZbjWAAEDih4.jpg https://t.co/V8e54eK8Te https://twitter.com/Chicos/status/1243862550518915074/photo/1 photo http://pbs.twimg.com/media/EUMWZbjWAAEDih4.jpg https://t.co/V8e54eK8Te https://twitter.com/Chicos/status/1243862550518915074/photo/1 NA NA NA en NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA c(NA, NA) c(NA, NA) c(NA, NA, NA, NA, NA, NA, NA, NA) https://twitter.com/NA/status/1243862550518915074 Chico’s® Chic is in the details. https://t.co/VlGuJy7hFJ FALSE 44218 1007 406 7521 3898 1251755078 TRUE https://t.co/VlGuJy7hFJ http://www.chicos.com NA https://pbs.twimg.com/profile_banners/70509424/1583166539 http://abs.twimg.com/images/themes/theme1/bg.png http://pbs.twimg.com/profile_images/1234516055034343424/v797_blq_normal.jpg
412795908 1243313256644644866 1585263954 chicoflores

And the results are in…

As voted by the #JackArmy, our greatest away win 👇

🏆 Valencia 0-3 Swansea 🏆

Thanks for voting! https://t.co/6RHf8bVini
Twitter for iPhone 140 NA NA NA FALSE TRUE 0 14 NA NA JackArmy NA NA NA NA NA NA NA NA NA NA NA NA 472385266 SwansOfficial en NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 1243217005819367425

And the results are in…

As voted by the #JackArmy, our greatest away win 👇

🏆 Valencia 0-3 Swansea 🏆

Thanks for voting! https://t.co/6RHf8bVini
1585241006 Twitter Web App 256 14 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA c(NA, NA) c(NA, NA) c(NA, NA, NA, NA, NA, NA, NA, NA) https://twitter.com/NA/status/1243313256644644866 Chico Flores Madrid,Spain. Official Twitter of Chico Flores Fuenlabrada FC Player https://t.co/w77xlzMPI8 FALSE 250118 749 311 11318 16004 1321330389 TRUE https://t.co/w77xlzMPI8 http://www.chicoflores.com NA https://pbs.twimg.com/profile_banners/412795908/1530614748 http://abs.twimg.com/images/themes/theme5/bg.gif http://pbs.twimg.com/profile_images/1177760211853074432/mQzszkho_normal.jpg
48035012 1243998871501537280 1585427418 chicoharlan This is a singular story of suffering, one among thousands and thousands, which makes this pandemic all that much more unimaginable. https://t.co/kRPzJGQcUf Twitter for iPhone 140 NA NA NA FALSE TRUE 0 51 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 121857220 elisaslow en NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 1243987959231033356 This is a singular story of suffering, one among thousands and thousands, which makes this pandemic all that much more unimaginable. https://t.co/kRPzJGQcUf 1585424816 Twitter Web App 78 51 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA c(NA, NA) c(NA, NA) c(NA, NA, NA, NA, NA, NA, NA, NA) https://twitter.com/NA/status/1243998871501537280 Chico Harlan Rome, Lazio Rome bureau chief for @washingtonpost, covering Southern Europe. Previously in Seoul, Tokyo, Sydney, and the Nationals Park press box. Instagram: chicoharlan https://t.co/7LHGpxYgem FALSE 17642 314 912 7068 48 1245258951 TRUE https://t.co/7LHGpxYgem https://www.washingtonpost.com/people/chico-harlan/ NA https://pbs.twimg.com/profile_banners/48035012/1524836476 http://abs.twimg.com/images/themes/theme1/bg.png http://pbs.twimg.com/profile_images/1242731450597851139/hxL3Tzpm_normal.jpg
1227722562 1243936892606971904 1585412641 TopoChicoUSA At Poison Girl, noon-8pm daily. Curbside service for hot dogs, beer and booze. @topochicousa @jimbeamofficial Come and get it. #poisongirllovesyou #littledipperlovesyou #supportbartenders https://t.co/E8P858VqCL Twitter for iPhone 139 NA NA NA FALSE TRUE 0 1 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA c(“161703581”, “1227722562”, “2518552490”) c(“antidotecoffee”, “TopoChicoUSA”, “jimbeamofficial”) en NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 1243613184839335937 At Poison Girl, noon-8pm daily. Curbside service for hot dogs, beer and booze. @topochicousa @jimbeamofficial Come and get it. #poisongirllovesyou #littledipperlovesyou #supportbartenders https://t.co/E8P858VqCL 1585335463 Twitter for iPhone 3 1 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA c(NA, NA) c(NA, NA) c(NA, NA, NA, NA, NA, NA, NA, NA) https://twitter.com/NA/status/1243936892606971904 Topo Chico Texas, USA Official account of Topo Chico Agua Mineral in the USA. (888) 456-4357 https://t.co/nm3IphSiTJ FALSE 10512 1949 68 5559 5963 1362063735 FALSE https://t.co/nm3IphSiTJ http://www.TopoChicoUSA.net NA https://pbs.twimg.com/profile_banners/1227722562/1471141047 http://abs.twimg.com/images/themes/theme1/bg.png http://pbs.twimg.com/profile_images/1194459337697177600/GbXtcuj5_normal.jpg
118247024 1243596639073427458 1585331518 chicoBean How long y’all think we gon be…. https://t.co/Du4GpCeW6r Twitter for iPhone 34 NA NA NA FALSE FALSE 40562 10931 NA NA NA NA NA NA NA http://pbs.twimg.com/media/EUIkjHEXYAAWY9Q.jpg https://t.co/Du4GpCeW6r https://twitter.com/chicoBean/status/1243596639073427458/photo/1 photo http://pbs.twimg.com/media/EUIkjHEXYAAWY9Q.jpg https://t.co/Du4GpCeW6r https://twitter.com/chicoBean/status/1243596639073427458/photo/1 NA NA NA en NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA c(NA, NA) c(NA, NA) c(NA, NA, NA, NA, NA, NA, NA, NA) https://twitter.com/NA/status/1243596639073427458 Chico Bean ÜT: 36.061243,-79.844903 Comedian/Actor/Producer, 1/4 of The Freestyle Funny Comedy Show, Newest Cast Member of @MTV2 WildnOut Season 5, for booking contact https://t.co/iJHZjJb23P FALSE 211847 659 127 13926 191 1267323091 TRUE https://t.co/iJHZjJb23P http://www.chicobean.com NA https://pbs.twimg.com/profile_banners/118247024/1524704621 http://abs.twimg.com/images/themes/theme1/bg.png http://pbs.twimg.com/profile_images/989308997458067456/Ax8Lh9xy_normal.jpg
3314757086 1241149321023639552 1584748032 ChicoFD Today our recruit firefighters trained on search and rescue techniques at the Fire Training Center. In this picture, you see the thermal imaging outline of a recruit firefighter as he works through zero visibility conditions. #ChicoFD #ChicoFire https://t.co/vaoNvzljzM Twitter for iPhone 245 NA NA NA FALSE FALSE 11 1 NA NA c(“ChicoFD”, “ChicoFire”) NA NA NA NA http://pbs.twimg.com/media/ETlytxJU0AEqcee.jpg https://t.co/vaoNvzljzM https://twitter.com/ChicoFD/status/1241149321023639552/photo/1 photo http://pbs.twimg.com/media/ETlytxJU0AEqcee.jpg https://t.co/vaoNvzljzM https://twitter.com/ChicoFD/status/1241149321023639552/photo/1 NA NA NA en NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA https://api.twitter.com/1.1/geo/id/9b381228ebd9a499.json Chico Chico, CA city United States US c(NA, NA) c(NA, NA) c(-121.919174, -121.748286, -121.748286, -121.919174, 39.676071, 39.676071, 39.818003, 39.818003) https://twitter.com/NA/status/1241149321023639552 Chico Fire Department Chico, CA Official Twitter page of the Chico Fire Department. Call 911 in an emergency. https://t.co/RmoL8t706t FALSE 9823 111 120 1614 333 1439529158 TRUE https://t.co/RmoL8t706t http://www.chico.ca.us/fire/home_page.asp NA https://pbs.twimg.com/profile_banners/3314757086/1523856896 http://abs.twimg.com/images/themes/theme1/bg.png http://pbs.twimg.com/profile_images/689132857298518016/Kw-Bvwdu_normal.jpg
539247527 1243351640259719169 1585273106 ChicoChamber CARD is opening a program to provide childcare options for children of critical workers who do not have access to typical care because of COVID-19 closures. Details in the link. https://t.co/QEGpWyvuIB Twitter Web App 140 NA NA NA FALSE TRUE 0 5 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 43942200 ActionNewsNow en NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 1243252636348006409 CARD is opening a program to provide childcare options for children of critical workers who do not have access to typical care because of COVID-19 closures. Details in the link. https://t.co/QEGpWyvuIB 1585249501 SocialNewsDesk 9 5 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA c(NA, NA) c(NA, NA) c(NA, NA, NA, NA, NA, NA, NA, NA) https://twitter.com/NA/status/1243351640259719169 Chico Chamber Chico, CA The Chico Chamber is one of the largest business advocacy organizations in Northern California. Leveraging the voice of business for community good. http://t.co/wyLQT6WiKS FALSE 2112 583 48 4298 7451 1332957263 TRUE http://t.co/wyLQT6WiKS http://www.chicochamber.com NA https://pbs.twimg.com/profile_banners/539247527/1580760666 http://abs.twimg.com/images/themes/theme3/bg.gif http://pbs.twimg.com/profile_images/955590292253556736/vgxvOd0M_normal.jpg
119115558 1242225460647321601 1585004603 ChicoPolice Follow the link to the City of Chico COVID 19 Press Briefing and information regarding public safety response. https://t.co/M0cjYtlAPM. @ChicoCityof @ChicoFD #chicoca #COVID19 #SocialDistancing #publicsafety https://t.co/0Cm9Y6s389 Twitter for iPhone 207 NA NA NA FALSE FALSE 1 1 NA NA c(“chicoca”, “COVID19”, “SocialDistancing”, “publicsafety”) NA facebook.com/OfficialCityof… https://t.co/M0cjYtlAPM https://www.facebook.com/OfficialCityofChico/videos/875224276249889/?vh=e&d=n http://pbs.twimg.com/media/ET1FeG9U8AA5Wu5.jpg https://t.co/0Cm9Y6s389 https://twitter.com/ChicoPolice/status/1242225460647321601/photo/1 photo http://pbs.twimg.com/media/ET1FeG9U8AA5Wu5.jpg https://t.co/0Cm9Y6s389 https://twitter.com/ChicoPolice/status/1242225460647321601/photo/1 NA c(“1235295913494450176”, “3314757086”) c(“ChicoCityof”, “ChicoFD”) en NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA https://api.twitter.com/1.1/geo/id/9b381228ebd9a499.json Chico Chico, CA city United States US c(NA, NA) c(NA, NA) c(-121.919174, -121.748286, -121.748286, -121.919174, 39.676071, 39.676071, 39.818003, 39.818003) https://twitter.com/NA/status/1242225460647321601 Chico Police Dept. Chico Ca. The official account of the Chico Police Department. Our mission: enhance and facilitate community safety. Site is NOT monitored 24 hours. 9-1-1 for emergencies https://t.co/8tJO2mQxHE FALSE 7000 655 139 1449 1229 1267554458 TRUE https://t.co/8tJO2mQxHE http://www.ci.chico.ca.us/police/home_page.asp NA https://pbs.twimg.com/profile_banners/119115558/1579224238 http://abs.twimg.com/images/themes/theme14/bg.gif http://pbs.twimg.com/profile_images/1072255407899369472/LEnb9fL7_normal.jpg

Code Example

This is an example of using the search_users() to find 10 users with the query term “chico”. This is the same function call we made above to show the results in the table.

search_users("chico", n = 10, verbose = FALSE)

Measuring User Activity

We will now run that same query again and save the results we save into a variable called statuses so that we can further manipulate the data.

statuses <- search_users("chico", n=10, verbose = FALSE)

We can use this data in a variety of ways. One thing we can do is visualize the number of lifetime tweets for individual users.

ggplot(statuses, mapping = aes(y = statuses_count, x = screen_name )) + theme(axis.text.x=element_text(angle=60, size = 7, vjust = 0.5)) + geom_bar(stat="identity", fill="steelblue") + labs(y = "Total Tweets", x = "Screen Name") + ggtitle("User Tweet Totals")

We can use the lifetime tweets and the date that they created their account to determine how active each user is (tweets/time). First, we can create a new column to represent the number of weeks that a person has had their account.

statuses <- statuses %>% select(statuses_count, account_created_at, screen_name)

statuses$timeHadAccount <- sapply(statuses$account_created_at, function(x){
  d <- as.POSIXct(Sys.time())
  d2 <- as.POSIXct(format(d, tz="UTC", usetz=TRUE), tz="UTC")
  return(difftime(d2, x, units ="weeks"))
})

Next, we can get the ratio of how active each user is (avg number of tweets per week)

statuses$activity <- statuses$statuses_count/statuses$timeHadAccount

Now, we can plot this data to show each user’s level of activity.

ggplot(statuses, mapping = aes(y = activity, x = screen_name )) + theme(axis.text.x=element_text(angle=60, size = 7, vjust = 0.5)) + geom_bar(stat="identity", fill="steelblue") + labs(y = "Ave Tweets per Week", x = "Screen Name") + ggtitle("User Activity Chart")

Who Mentioned the Corona Virus?

In this example, I will show how you can use simple pattern matching to find users for the search term corona virus and specifically mentioned the words “death, dead, infected” in their text description.

users <- search_users("corona virus", n=100, verbose = FALSE)

The code below creates a new column in the data. A 1 will be placed in the column for that user if they mentioned any of the words we are searching for, and a 0 if they did not.

sick <- c("death|dead|infected")
users$corona_virus <- sapply(users$text, function(x){
  if(any(grepl(sick, x))){  
     return(1)
  }
  return(0)
})

Now, we can see how many of the 100 users wrote those words.

sum(users$corona_virus)
## [1] 4

This shows us that 3 people mentioned at least one of the three words. We can also see the specific message they sent.

users$text[users$corona_virus == 1]
## [1] "#Breaking: US coronavirus death toll hits 2,000"                                                                                                                                        
## [2] "US #CoronaVirus: To date, a total of 120,076 cases and 1,993 total deaths and #COVID19 deaths have been confirmed in the United States.\n\n#CoronaVirusOutbreak https://t.co/9Wt8WohlNY"
## [3] "ITALY has TODAY confirmed 889 new death cases from coronavirus. Total deaths in Italy now is 10023."                                                                                    
## [4] "#NewsAlert |  Iran reports 139 new #coronavirus deaths, raising total to 2,517,\n\n#CoronavirusOutbreak #CoronaUpdate"

Or, if we want to see only the username of the users who said these words in their messages we can use this code:

users$screen_name[users$corona_virus == 1]
## [1] "CoronaNumbers"  "CoronaVirusNow" "Obuchunju"      "21daylockdown"