作者blueevil ()
看板R_Language
标题[问题] 依照消费状况分类
时间Mon Oct 5 14:52:03 2015
[问题类型]:
我可以分类成功,想请问有比较简洁的方法吗?或别的方法?谢谢分享!
[问题叙述]:
要将消费者依照消费年份的状态分成三类:回购顾客,新增顾客,流失顾客
[程式范例]:
http://ideone.com/RJ4ixk
number=50
data <- matrix(nrow = number, ncol = 3 )
colnames(data) <- c("ID", "Shop_year", "Region")
set.seed(1)
data[,1] <- c(sample(1:25, size=number, replace = T) )#ID
data[,2] <- c(sample(c("year2013","year2014","year2015"), size=number, replace= T, prob = c(0.1, 0.3, 0.6) ) )
data[,3] <- c(sample(c("北区一","北区二","北区三"), size = number,
replace = T, prob = c(0.5,0.3,0.2) ))
data <- data.frame(data)
library(reshape2)
library(reshape)
library(dplyr)
result<- cast(data, ID~Shop_year,value=c("Region"))
##分类成三组
data_new <- filter(result, year2015 !=0 & year2014 == 0)
data_new$level <- "新增"
data_lost <- filter(result, year2015 == 0)
data_lost$level <- "流失"
data_now <- filter(result, year2015 != 0 & year2014 !=0)
data_now$level <- "回购"
##合并档案
data_combine <- rbind.data.frame(data_now, data_new, data_lost)
## 新增分组资料到原始档案
data_final <- merge(data, data_combine)
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 59.120.192.175
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1444027929.A.FAD.html
※ 编辑: blueevil (59.120.192.175), 10/05/2015 15:01:52
2F:→ celestialgod: 上半部是把你的code更正到可以运作 10/05 15:29
3F:→ celestialgod: 下半部是用套件改写 10/05 15:29
4F:→ celestialgod: 我不知道你filter怎麽可以跑的XDD 10/05 15:30
5F:→ blueevil: 是用 dplyr. 抱歉我忘了加上library(dplyr) 10/05 15:47
6F:→ blueevil: 谢谢您!!! 10/05 15:48
※ 编辑: blueevil (59.120.192.175), 10/05/2015 16:33:39
8F:→ psinqoo: 我猜你可能也需要做 RFM模型 10/07 08:29
9F:→ blueevil: 谢谢p大分享!!! 10/07 10:30