While following this very interesting tutorial (https://rpubs.com/hrbrmstr/customer-segmentation-r), i came across an error i dont really understand.
Here is the piece of code resulting in the message 'Error: Value column 'n' does not exist in input.' in Rstudio 1.0.136:
library(readxl)
library(dplyr)
library(tidyr)
library(viridis)
library(ggplot2)
library(ggfortify)
url <- "http://blog.yhathq.com/static/misc/data/WineKMC.xlsx"
fil <- basename(url)
if (!file.exists(fil)) download.file(url, fil)
offers <- read_excel(fil, sheet = 1)
colnames(offers) <- c("offer_id", "campaign", "varietal", "min_qty", "discount", "origin", "past_peak")
head(offers, 12)
transactions <- read_excel(fil, sheet = 2)
colnames(transactions) <- c("customer_name", "offer_id")
transactions$n <- 1
head(transactions)
left_join(offers, transactions, by="offer_id") %>%
count(customer_name, offer_id, wt=n) %>%
spread(offer_id, n) %>%
mutate_each(funs(ifelse(is.na(.), 0, .))) -> dat
The line before last is the one creating the issue.
Anybody would know why?