2017-04-17 13 views
-1

이 매우 흥미로운 자습서 (https://rpubs.com/hrbrmstr/customer-segmentation-r)를 따르는 동안 나는 정말로 이해하지 못하는 오류를 발견했습니다.클러스터 계산 자습서 - 확산 관련 문제

'오류 : 값 열'이 (가) '입력에 존재하지 않습니다.'라는 메시지가 표시되는 코드는 다음과 같습니다. 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 

마지막 줄 앞에 문제가 발생합니다.

누구나 알 수 있나요?

+1

일반적으로 몇 년 내에 깨지기 쉬운 링크를 사용하는 대신 재현 할 수있는 예제를 게시해야합니다. 몇 가지 지침 : http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/28481250#28481250 물론, R을 넘어서 어떤 도구를 사용하고 있는지 확인해야합니다 그 자체. 'spread'는 R에없는 것입니다. – Frank

+0

물론, 제 잘못, 재현 가능한 예제로 원본 게시물을 수정했습니다. – Romain

+0

감사합니다. 데이터 용 블로그가 필요한 경우 장기간 재현 할 수 없습니다. 아마도 dplyr 만 필요할 것으로 예상되는 경우이 모든 패키지를로드하는 경우 최소한이 아닙니다. 이상적인 것은 [mcve]입니다. 어쨌든,'count' 스텝이'n'이라는 이름의 컬럼을 생성 하는지를 보아서 직접 디버깅을 시작할 수 있습니다. – Frank

답변

0

?dplyr::count의 매뉴얼 페이지를 살펴 준비해주십시오

Note

The column name in the returned data is usually n, even if you have supplied a weight.

If the data already already has a column named n, the output column will be called nn. If the table already has columns called n and nn then the column returned will be nnn, and so on.

이 경우는, 원래의 데이터는 이미 n라는 열이 때문에 count 후 새 열 nn를 호출 할 것이다. 따라서 spread(offer_id, n) %>%spread(offer_id, nn) %>%으로 변경해야합니다. 이 자습서는이 변경 전에 작성되었을 수 있습니다.