To extract sample tweets using the rtweet package in R, you can use the search_tweets() function with specific parameters to define your search criteria. Here's an example of how to extract sample tweets:
# Load the rtweet package
library(rtweet)
# Authenticate with Twitter API (replace XXXX with your own API keys)
api_key <- "XXXX"
api_secret_key <- "XXXX"
access_token <- "XXXX"
access_token_secret <- "XXXX"
token <- create_token(
app = "YourApp", # Name of your app
consumer_key = api_key,
consumer_secret = api_secret_key,
access_token = access_token,
access_secret = access_token_secret
)
# Search for tweets containing a specific keyword (replace "keyword" with your search query)
tweets <- search_tweets("keyword", n = 100) # Extract 100 tweets containing the keyword
# View the structure of the tweets
str(tweets)
# View the first few rows of the data
head(tweets)
In this example:
Replace "keyword" with the term you want to search for in tweets.
Adjust the n parameter in search_tweets() to specify the number of tweets you want to extract.
The search_tweets() function will return a data frame containing various information about the retrieved tweets, such as tweet text, user information, tweet creation time, etc.
Make sure to replace "XXXX" with your actual API keys obtained from your Twitter Developer account.
Remember to adhere to Twitter's usage policy and rate limits when using the Twitter API.