Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
library(data.table)
library(tidyverse) # ggplot2, tidyr, dplyr, purrr, stringr ...
library(scales)
library(grid)
library(RColorBrewer)
###############################################################
## MOST COMMON ACTIONS DURING FIRST TWO WEEKS
###############################################################
## IMPORT AND TIDY DATA
path <- "~/Downloads/"
files <- str_c(path, c("small_user_actions.csv", "churned_users_actions.csv"))
read_data_and_assign_status <- function(file, status) {
read.table(file, header = TRUE, sep = ",") %>%
mutate(user_type = status) %>%
rename(user_id = names(.)[1],
action = names(.)[2],
count = names(.)[3])
}
# read data, get better column names, add user_type
user_actions <- map2_dfr(files, c("healthy", "churned"), read_data)
# create a summary data
total_by_action <- user_actions %>%
group_by(action, user_type) %>%
summarise(n_users = n_distinct(user_id),
total_count = sum(count)) %>%
mutate(count_per_user = total_count / n_users)
save(total_by_action, file="~/Google Drive/total_by_action.Rda")
## TARGET ACTIONS - PLOT TOTAL ACTION COUNTS PER USER
target_actions <- map(0:4, function(i) { #<<
total_by_action %>%
arrange(desc(total_count)) %>%
slice(1:10 + 10 * i) %>% #<<
pull(action)
})
## HORIZONTAL BAR PLOT OF TOP ACTIONS (BY 10)
action_bar_plot <- map(1:5, function(i) {
total_by_action %>%
filter(action %in% target_actions[[i]]) %>% #<<
ggplot(aes(x = action, y = count_per_user, fill = user_type)) +
geom_col(position = "dodge") +
coord_flip() +
buffer_theme() +
labs(x = "", y = "Average Actions Per User")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment