-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_data.R
58 lines (43 loc) · 1.43 KB
/
get_data.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Retrieve data from twitter (Alexander Stubb tweets)
# auth: [email protected]
# 01.02.2016
# Get the data
# -----------
source("utility/functions.R")
access_libraries()
tweeter <- "alexstubb"
timezone <- 2
tweeter_languages <- c("english","finnish","swedish")
filepath <- paste0("data/",tweeter,"_tweets.Rda")
# authetication to twitter
setup_twitter_oauth(consumer_key = "",
consumer_secret = "",
access_token = "",
access_secret = "")
# call wrapper for twitteR library search functions
tw = super_search(search = "userTimeline", stopper=0,
args=list(user=tweeter, n = 20))
tw = twListToDF(tw) # to data.farme
# Data cleaning and variable engineering
# -------------------------------------
# remove duplicate tweets and change timezone
tw$id <- as.numeric(tw$id)
tw <- tw[!duplicated(tw$id),]
tw$created <- tw$created + timezone*60*60
tw$tunti <- as.numeric(format(tw$created,"%H"))
# define popularity as a sum of favorites and retweets
tw$suosio <- tw$favoriteCount + tw$retweetCount
# clean the tweets and add language variable
tw$tweet <- clean_text(tw$text)
tw <- tw[tw$tweet !="",]
tw$lang <- get_languages(tw$tweet, tweeter_languages)
tw <- tw[!is.na(tw$lang),]
# some basic stats
mindate <- format(min(tw$created),"%d.%m.%Y")
maxdate <- format(max(tw$created),"%d.%m.%Y")
ntweets <- nrow(tw)
ntweets
mindate
maxdate
# save to file
save(file=filepath,tw)