Assignment2022.pdf
API Key -
aPBc1ItVUF9INorSmM0HG7ciU
API secret Key
I5Dnpxre9omatNIWqiHWk1Slf4tTk7biWZWzp4Yo6y50pTSOhD
Bearer Token
AAAAAAAAAAAAAAAAAAAAADR2dQEAAAAAFxVD58AS%2Fgl9RbFUjTBSbfBARt0%3DEuzaFf7M7Pr8z0tsVJpb8lI3EieowzsLpwayAXLq89wCu2dYFO
App Name
Joel_SMI
APP ID
XXXXXXXXXX
#Sample codes#
install.packages("rtweet")
li
ary("rtweet")
## Use your own key and secret!
## Don't share your key and secret with others!
key = ""
secret = ""
create_token(
app = "my_twitter_app",
consumer_key = key,
consumer_secret = secret)
tweets = search_tweets("elon musk", n = 100, lang = "en")
API Key -
aPBc1ItVUF9INorSmM0HG7ciU
API secret Key
I5Dnpxre9omatNIWqiHWk1Slf4tTk7biWZWzp4Yo6y50pTSOhD
Bearer Token
AAAAAAAAAAAAAAAAAAAAADR2dQEAAAAAFxVD58AS%2Fgl9RbFUjTBSbfBARt0%3DEuzaFf7M7Pr8z0tsVJpb8lI3EieowzsLpwayAXLq89wCu2dYFO
App Name
Joel_SMI
APP ID
XXXXXXXXXX
#Sample codes#
install.packages("rtweet")
install.packages("Matrix")
install.packages("sna")
install.packages("igraph")
li
ary("rtweet")
li
ary("Matrix")
li
ary("sna")
li
ary("igraph")
key = "aPBc1ItVUF9INorSmM0HG7ciU"
secret = "I5Dnpxre9omatNIWqiHWk1Slf4tTk7biWZWzp4Yo6y50pTSOhD"
create_token(
app = "Joel_SMI",
consumer_key = key,
consumer_secret = secret)
##see if these four tweets can use better search terms related to elon musk and twitter as the
ones given are pretty basic ones
tweets1 = search_tweets("elonmusk", n = 50, lang = "en", include_rts = FALSE)
tweets2 = search_tweets("elonmusk AND twitter", n = 100, lang = "en")
tweets3 = search_tweets("elonmusk BUY twitter", n = 100, lang = "en")
tweets4 = search_tweets("elonmusk OR twitter", n = 50, lang = "en")
## join them into one dataframe
all_tweets =
ind(tweets1,tweets2,tweets3,tweets4)
## create from-to data frame representing retweet/mention
eply connections
#tweets_net <- network_data(tweets, "retweet,mention,reply")
tweets_net = network_data(all_tweets,"mention")
## view edge data frame
tweets_net
## view user_id->screen_name index
all_names = as.data.frame(attr(tweets_net, "idsn"))
## (1) convert directly to graph object representing semantic network
tweets_graph <- network_graph(all_tweets,"mention")
## (2) plot graph via igraph.plotting
plot(tweets_graph)
## Use plot.igraph parameters to make your graph readable
plot(tweets_graph,edge.a
ow.mode='-',vertex.size = 10,vertex.label.cex = 0.7,layout=layout.fruchterman.reingold)
## Convert to undirected
tweets_graph_un = as.undirected(tweets_graph,mode = c("collapse"))
plot(tweets_graph_un,vertex.size = 10,vertex.label.cex = 0.7,layout=layout.fruchterman.reingold)
## Also try simplify to remove loops or multiple edges
## simplify(tweets_graph,remove.multiple = TRUE,remove.loops = TRUE,)
## Create a separate graph for each component of a graph.
components = decompose(tweets_graph)
sapply(components, diameter)