Skip to content

Instantly share code, notes, and snippets.

View MarkEdmondson1234's full-sized avatar
🦑
Tappity tap tap

Mark Edmondson MarkEdmondson1234

🦑
Tappity tap tap
View GitHub Profile
## functions.R
library(rga)
library(dygraphs)
library(zoo)
library(tidyr)
library(lubridate)
library(d3heatmap)
library(dplyr)
library(stringr)
library(DT)
@MarkEdmondson1234
MarkEdmondson1234 / downloadSearchAnalytics.R
Last active March 2, 2016 13:49
demo on how to download and archive search analytics data using searchConsoleR
## A script to download and archive Google search analytics
##
## Demo of searchConsoleR R package.
##
## Version 1 - 10th August 2015
##
## Mark Edmondson (http://markedmondson.me)
## load the required libraries
## (Download them with install.packages("googleAuthR") and install.packages("searchConsoleR" if necessary
@MarkEdmondson1234
MarkEdmondson1234 / combineCSVfiles.R
Created August 10, 2015 08:04
combine all csv files in a folder
## assuming you have a folder full of .csv's to merge
## csv's must all have identical column names.
folder <- "./path/to/csv/files"
filenames <- list.files(folder)
all_files <- Reduce(rbind, lapply(filenames, read.csv))
@MarkEdmondson1234
MarkEdmondson1234 / url_short.R
Last active August 29, 2015 14:27
Demo using googleAuthR to create a Google API package
library(googleAuthR)
## change the native googleAuthR scopes to the one needed.
options("googleAuthR.scopes.selected" =
c("https://www.googleapis.com/auth/urlshortener"))
#' Shortens a url using goo.gl
#'
#' @param url URl to shorten with goo.gl
#'
@MarkEdmondson1234
MarkEdmondson1234 / shiny_url_short.R
Created August 19, 2015 07:47
Demo using Shiny with googleAuthR
## in global.R
library(googleAuthR)
options("googleAuthR.scopes.selected" = c("https://www.googleapis.com/auth/urlshortener"))
shorten_url <- function(url){
body = list(
longUrl = url
)
install.packages("googleAuthR")
library(googleAnalyticsR_public)
gar_auth(new_user=T)
## your profile view Id
id <- "XXXXXX"
## 61607 results
## 30049 unique Ids
## 185 Sku's
## want: 30049 x 187
## userId, product1_view, product2_view, ...., productN_view, productBought
pv <- reshape2::recast(product_views,
dimension1 ~ productSku + variable,
fun.aggregate=sum)
library(dplyr)
## if a user buys more than once, the row will be duplicated
pt <- product_trans %>% select(productSku, dimension1)
library(randomForest)
## warning - can take a long time (30mins)
rf <- randomForest(x = predictors, y = response)
## once model done, we run it using test data and compare results to reality
predictor_test <- test[,which(!names(test) %in% c("dimension1","boughtSku"))]
response_test <- as.factor(test[,"boughtSku"])
## check result on test set
prediction <- predict(rf, predictor_test)
## function to get plot data format
getCompareTable <- function (test_data, prediction) {
require(dplyr)
## plot real vs model bought Sku
actual_freq <- table(model_data$boughtSku)
predicted_freq <- table(prediction)
actual_freq <- actual_freq[order(actual_freq)]
predicted_freq <- predicted_freq[order(predicted_freq)]