require(lubridate) # Create date range date_start <- as.Date("2014-01-01") date_end <- as.Date("2014-12-01") drange <- as.integer(date_end - date_start) drange <- date_start + days(0:d) issues <- data.frame(date = drange) issuelist <- xmlToList("issues.xml") issueheads <- names(issuelist) issues[issueheads] <- 0 for(d in 1:nrow(issues)) { curdate <- issues$date[d] cat(as.character(curdate),"\n") # Put all tweets from specific day in a temporary DF tweets_curday <- tweets[tweets[, "created_at"] == curdate, ] for(t in 1:nrow(tweets_curday)){ # Select tweet's text, make it lowercase and remove hashtag indicators (#) curtext <- tolower(as.character(tweets_curday$text[t])) curtext <- str_replace_all(curtext, "#", "") for(i in 1:length(issuelist)) { curtags <- as.character(issuelist[[i]]) curissue <- names(issuelist)[i] curtags <- str_c("\\W", curtags, "\\W") tags_found <- str_detect(curtext, sprintf("%s", curtags)) tags_found <- any(tags_found) if(tags_found) { #cat("Positive in", curissue,"from",as.character(drange[d]),"\n") issues[d,curissue] <- issues[d,curissue] + 1 } else { #cat("Nothing found\n") } } # /for issuelist } # /for tweets_curday } # /for drange # VISUALS ----------------------------------------------------------------- library(ggplot2) library(reshape2) df <- melt(issues,id="date") ggplot(df,aes(x=date,y=value,colour=variable,group=variable)) + geom_line() ggplot(df,aes(x=date,y=value,colour=variable,group=variable)) + geom_smooth(size=0.5,method="loess",formula = y ~ x, se=FALSE) # POSSIBLY USEFUL CODE ---------------------------------------------------- # Limits of list length(issuelist) length(issuelist[[2]]) # Select all tweets from current day in drange tweets_curday <- tweets[tweets[, "created_at"] == drange[5], ] # Is column a issue counting column? str_detect(names(issues[2]), "^issue")