This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Cmd+Shift+Enter.

Install required packages

# install.packages("readr")
# install.packages("plyr")
# install.packages("stringr")
# install.packages("stringi")
# install.packages("magrittr")
# install.packages("dplyr")
# install.packages("plotly")

library(readr)
## Warning: package 'readr' was built under R version 3.4.4
library("plyr")
## Warning: package 'plyr' was built under R version 3.4.4
library(plotly)
## Warning: package 'plotly' was built under R version 3.4.4
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.4.4
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following objects are masked from 'package:plyr':
## 
##     arrange, mutate, rename, summarise
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

User gender distribution by city

# Load data
Trump <- read_csv("Trump.csv")
## Warning: Missing column names filled in: 'X1' [1]
## Parsed with column specification:
## cols(
##   X1 = col_integer(),
##   MESSAGE_BODY = col_character(),
##   MESSAGE_COUNTRY = col_character(),
##   MESSAGE_FAVORITES_COUNT = col_integer(),
##   MESSAGE_LOCATION = col_character(),
##   MESSAGE_LOCATION_DISPLAY_NAME = col_character(),
##   MESSAGE_POSTED_TIME = col_datetime(format = ""),
##   MESSAGE_RETWEET_COUNT = col_integer(),
##   USER_CITY = col_character(),
##   USER_COUNTRY = col_character(),
##   USER_DISPLAY_NAME = col_character(),
##   USER_FOLLOWERS_COUNT = col_integer(),
##   USER_FRIENDS_COUNT = col_integer(),
##   USER_GENDER = col_character(),
##   USER_LOCATION_DISPLAY_NAME = col_character(),
##   USER_SCREEN_NAME = col_character()
## )
Trump$country <- tolower(Trump$USER_COUNTRY)

# Get subset of the data to explore
subdf <- subset(Trump, country =='united states' | country =='australia' | country =='canada' | country =='india' | country =='united kingdom')
followerCountry <-data.frame(table(subdf[,c("country", "USER_GENDER")]))
xtabs(Freq~country+USER_GENDER,followerCountry)
##                 USER_GENDER
## country          female male unknown
##   australia           7   10      13
##   canada             10   32      28
##   india               2    7       1
##   united kingdom     13   55      52
##   united states     487 1011    1166
tab = reshape(followerCountry,direction="wide",timevar="USER_GENDER",idvar="country")

p <- plot_ly(tab, x = ~country, y = ~Freq.female, type = 'bar', name = 'Female') %>%
  add_trace(y = ~Freq.male, name = 'Male') %>%
  layout(yaxis = list(title = 'Count'), barmode = 'group')

p
## Warning: package 'bindrcpp' was built under R version 3.4.4

User posting time by gender

Trump$days <- weekdays(as.POSIXlt(Trump$MESSAGE_POSTED_TIME))
dfrm <-data.frame(table(Trump[,c("USER_GENDER","days")]))
genderDays = reshape(dfrm,direction="wide",timevar="days",idvar="USER_GENDER")

p <- plot_ly(genderDays, x = ~USER_GENDER, y = ~Freq.Monday, type = 'bar', name = 'Monday') %>%
  add_trace(y = ~Freq.Tuesday, name = 'Tuesday') %>%
   add_trace(y = ~Freq.Wednesday, name = 'Wednesday') %>%
   add_trace(y = ~Freq.Thursday, name = 'Thursday') %>%
   add_trace(y = ~Freq.Friday, name = 'Friday') %>%
   add_trace(y = ~Freq.Saturday, name = 'Saturday') %>%
   add_trace(y = ~Freq.Sunday, name = 'Sunday') %>%
  layout(yaxis = list(title = 'Count'), barmode = 'group')

p

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Cmd+Option+I.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Cmd+Shift+K to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.