在遵循这个非常有趣的教程(https://rpubs.com/hrbrmstr/customer-segmentation-r)时,我遇到了一个我不太明白的错误。
这是导致消息“错误:输入中不存在值列'n'”的代码段。在 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
最后一行是造成问题的行。
有人会知道为什么吗?