Reputation: 1867
enter code hereIs it possible to look for two different hastags in one searchTwitter
command?
Example
my_h <- as.POSIXlt(Sys.time())
my_h <- strptime(my_h, format = "%Y-%m-%d %H:%M:%S", tz = "CET")
hrs <- function(u) {
x <- u * 3600
return(x)
}
my_h <- my_h - hrs(24)
my_h <- data.frame(day = strptime(my_h, "%Y-%m-%d", tz = ""))
I want to look for hastags #dn
and #park
I can do it separately as below
tweets<-twListToDF(searchTwitter("#dn", n=5000, since = as.character(my_h$day)))
write.table(tweets, "all_dn_tweets.csv", row.names = F, append = T, sep = ";", col.names = F)
tweets<-twListToDF(searchTwitter("#park", n=5000, since = as.character(my_h$day)))
write.table(tweets, "all_park_tweets.csv", row.names = F, append = T, sep = ";", col.names = F)
The question is if these two can be squeezed into one?
Upvotes: 0
Views: 4685
Reputation: 615
I found the answer posted here more elegant. For the present case it basically is
hashtags <- '#metallica + #slayer'
tweets <- searchTwitter(hashtags, n = 10, lang = 'en', retryOnRateLimit = 100)
tweetsDF <- twListToDF(tweets)
Upvotes: 1
Reputation: 54237
Try something like this
hashtags <- c("#metallica", "#slayer")
needle <- paste(hashtags, collapse = " OR ")
tweets <- searchTwitter(needle, n = 10)
df <- twListToDF(tweets)
for (hashtag in hashtags) {
write.csv(df[grep(hashtag, tolower(df$text), fixed = TRUE), ], paste0(hashtag, ".csv"))
}
Using tolower
on tweets may need some error handling - you'll find plenty of infos on that on the web.
Upvotes: 1