Searching Twitter Data with R and Grep

Searching Twitter Data with R and Grep

Searching Twitter Data with R and Grep

Learning how to use R Studio, R and then all the libraries and functions inside it can be hell(ish). But there’s good little ways to search your Twitter data for whatever you’re looking for, and give you some instant satisfaction in the process.

Step 1

You will need to install the following packages and then load the libraries of:

twitteR
dplyr

Step 2

Assuming you’ve got your data pulled off twitter and saved into a data frame so you can access it and it’s in a structured format, you can then begin the search process by using the following code:

#find a word in a df of twitter data

abuse_tweets <- filter(tweets.df, grepl(“abuse”, text))
tally(abuse_tweets)

This code uses the function filter() and inside it the grepl function which then looks for whatever keyword you want inside the double citation marks ” ” and you want to be looking inside the text field so you use the term text. You can then call the tally function and pass the tweets you’ve just searched through into it and it will return the result.

My result looks like this:

 n
1 4

This says n is the number of tweets returned and that number is 4. The grepl function returns a true or false answer and so counts the number of true or false responses. The grep school of functions are known as pattern matching and replacement functions or instructions to search and/or replace text or data.

The documentation for each package you use is invaluable. In this case:

TwitteR
dplyr

Happy searching!

 

 

 

 

 

 

 

 

 

The era of Lesbian Bed Death is over, long live Lesbian Fuck Eye

Sex is an art. And one that lesbians in particular have apparently, according to myth, taken a few decades to get their heads and legs around. Let alone actually in their beds. Today however, lesbian women have more orgasms, better sex and sex that lasts longer than...

Penetrating Real-Time Threat Behaviour: Cyber Analytics and the Pen Tester

It’s the wild, wild, west out there in cyberspace, except the feral camels[1] that once roamed Texas are the hackers, and they’re roaming beyond borders and through firewalls on the daily. At present, cyber threat intelligence gathering is a mish-mash of intrusion...
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><!-- [et_pb_line_break_holder] --><!-- left sidebar --><!-- [et_pb_line_break_holder] --><ins class="adsbygoogle"<!-- [et_pb_line_break_holder] --> style="display:block"<!-- [et_pb_line_break_holder] --> data-ad-client="ca-pub-2525169926381896"<!-- [et_pb_line_break_holder] --> data-ad-slot="1293035999"<!-- [et_pb_line_break_holder] --> data-ad-format="auto"<!-- [et_pb_line_break_holder] --> data-full-width-responsive="true"></ins><!-- [et_pb_line_break_holder] --><script><!-- [et_pb_line_break_holder] -->(adsbygoogle = window.adsbygoogle || []).push({});<!-- [et_pb_line_break_holder] --></script>