给大厨写的R数据分析代码

###************************************** 新老客户统计 ***************************************###
dachu <- read.csv("D:\\Dasktop\\bigdata_game\\天池\\大厨\\qijiandiankehu.csv", header = T, encoding = "utf-8", colClasses = c("character", "Date"))
str(dachu)
head(dachu,20)
temp <- table(dachu$买家昵称)
plot(table(sort(temp))/length(temp))
#library(data.table)
#month(dachu$下单日期[nrow(dachu)])

min(dachu$下单日期)
max(dachu$下单日期)

dachu$ym <- substr(dachu$下单日期, 1,7); head(dachu)
newcusts <- c()
oldcusts <- c()
ss <- sort(unique(dachu$ym))
#新客户满足一下两个条件:1)当月购买一次;2)之前无购买记录
#老客户满足一下两个条件之一:1)当月购买两次及以上;2)当月购买一次且之前有购买记录
for(i in 1:length(ss)){
  #date1 = as.Date(paste(substr(kk, 1, 6), paste(as.integer(substr(kk, 7, 7))+1,"-01", sep = ""), sep = ""))

  if(i == 1){
    date2 = as.Date(paste(ss[i+1], "-01", sep = ""))
    now = dachu$买家昵称[dachu$下单日期 < date2]
    temp = table(now)
    uniq = unique(now)
    newcusts = c(newcusts, sum(temp == 1))
    oldcusts = c(oldcusts, sum(temp > 1))
  }else if(i < length(ss)){
    date1 = as.Date(paste(ss[i], "-01", sep = ""))
    date2 = as.Date(paste(ss[i+1], "-01", sep = ""))
    now = dachu$买家昵称[(dachu$下单日期 < date2) & (dachu$下单日期 >= date1)]
    temp = table(now)
    #old_now = names(temp)[temp>1]
    new_now = names(temp)[temp==1]
    temp2 = table(c(uniq, new_now))
    newcusts = c(newcusts, (length(new_now) - sum(temp2 > 1)))
    #oldcusts = c(oldcusts, (length(old_now) + sum(temp2 > 1)))
    oldcusts = c(oldcusts, (length(temp) - length(new_now) + sum(temp2 > 1)))
    #uniq = unique(c(uniq, old_now, new_now))
    uniq = unique(c(uniq, names(temp)))

  }else{
    date1 = as.Date(paste(ss[i], "-01", sep = ""))
    now = dachu$买家昵称[dachu$下单日期 >= date1]
    temp = table(now)
    #old_now = names(temp)[temp>1]
    new_now = names(temp)[temp==1]
    temp2 = table(c(uniq, new_now))
    newcusts = c(newcusts, (length(new_now) - sum(temp2 > 1)))
    #oldcusts = c(oldcusts, (length(old_now) + sum(temp2 > 1)))
    oldcusts = c(oldcusts, (length(temp) - length(new_now) + sum(temp2 > 1)))
    #uniq = unique(c(uniq, old_now, new_now))
    uniq = unique(c(uniq, names(temp)))
  }

}
newcusts
oldcusts
(newcusts1 = cbind(date=ss, newcusts))
(oldcusts1 = cbind(date=ss, oldcusts))
write.csv(newcusts1, "C:\\Users\\hasee\\Desktop\\newcusts.csv",quote = F)
write.csv(oldcusts1, "C:\\Users\\hasee\\Desktop\\oldcusts.csv",quote = F)

#library(timeSeries)
win.graph()
opar <- par(no.readonly=TRUE)
par(lty=1, pch=1)  #par("cex") 查看默认值
# plot.ts(ts(newcusts+oldcusts, start = c(2014, 3), frequency = 12),main="薏凡特月度新老客户购买数量变化趋势", col=1)
# lines(ts(newcusts, start = c(2014, 3), frequency = 12), col=2)
# lines(ts(oldcusts, start = c(2014, 3), frequency = 12), col=3)
time <- seq.Date(as.Date("2014/3/1"), by = "month", length = length(ss))
plot(time, newcusts+oldcusts, xlab="月份", ylab="客户数", main="薏凡特月度新老客户购买数量变化趋势",
     type = "o", col=1)
# type画点/线, "p" for points, "l" for lines, "b" for both points and lines, "c" for empty points joined by lines,
# "o" for overplotted points and lines, "s" and "S" for stair steps and "h" for histogram-like vertical lines.
# Finally, "n" does not produce any points or lines.
# pch点型,
# cex点大小:
# lty线型:0=blank, 1=solid (default), 2=dashed, 3=dotted, 4=dotdash, 5=longdash, 6=twodash)
# lwd线宽
lines(time, newcusts, type = "o", col=2)
lines(time, oldcusts, type = "o", col=3)
legend("topright", c("总体客户", "新客户", "老客户"), col=1:3, lty=1, pch=1)
# “bottomright”, “bottom”, “bottomleft”, “left”, “topleft”, “top”, “topright”, “right”, “center”
par(opar)
#par(new=TRUE)

###************************************** 当月回购率 ***************************************###
# 月初统计购买一次的客户数,月末统计这部分人回购人数。
# 当月新进的客户且购买2次以上的不计入新客户
# 新客户可直接table=1的sum,但是当月回购的客户如何计算是难点。(可以unique内连接计数)
#数据导入
dachu <- read.csv("D:\\Dasktop\\bigdata_game\\天池\\大厨\\qijiandiankehu.csv", header = T, encoding = "utf-8", colClasses = c("character", "Date"))
str(dachu)

#定义保存新客户回购数据
new_customer <- data.frame()

min(dachu$下单时间)
max(dachu$下单时间)
ss=sort(unique(substr(dachu$下单时间,1,7)))

#从第二个月开始,首月新客数和回购数均为0
for(i in seq(length(ss))[-1]){
  data1 = as.Date(paste(ss[i], "-01", sep = ""))

  #月初之前客户购买记录
  data2 = max(i-12,1)
  data2 = as.Date(paste(ss[data2], "-01", sep = ""))
  temp <- table(dachu$买家昵称[(dachu$下单时间 >= data2)&(dachu$下单时间 < data1)])

  #月内客户购买记录
  if(i < length(ss)){
    data2 = as.Date(paste(ss[i+1], "-01", sep = ""))
    temp2 <- table(dachu$买家昵称[(dachu$下单时间 >= data1)&(dachu$下单时间 < data2)])
  }else{
    temp2 <- table(dachu$买家昵称[dachu$下单时间 >= data1])
  }

  #月内回购记录
  temp2 = merge(data.frame(k=names(temp)[temp==1]),
                data.frame(k=names(temp2)),
                by=c(‘k‘))

  #保存日期、月初新客数、月内回购数
  new_customer = rbind(new_customer, data.frame(date=ss[i], counts=sum(temp==1), repurchase=nrow(temp2)))

}
#计算回购率
new_customer$rate <- new_customer[[3]] / new_customer[[2]]
#colnames(new_customer) = c(‘date‘,‘counts‘,‘repurchase‘,‘rate‘)

win.graph()
opar<-par(mfrow=c(2,2))
plot(new_customer$date,new_customer$counts)
plot(new_customer$date,new_customer$repurchase);plot(new_customer$date,new_customer$rate)
par(opar)

write.csv(new_customer,"C:\\Users\\hasee\\Desktop\\new_customer.csv")

###****************************************** 季度转化率 ****************************************###
#数据导入
dachu <- read.csv("C:\\Users\\hasee\\Desktop\\qijiandiankehu.csv", header = T, encoding = "utf-8", colClasses = c("character", "Date"))
str(dachu)

#定义保存新客户回购数据
new_customer <- data.frame()

min(dachu$下单时间)
max(dachu$下单时间)
ss=sort(unique(substr(dachu$下单时间,1,7)))

#从第二个月开始,首月新客数和回购数均为0
for(i in seq(length(ss)-2)[-1]){
  data1 = as.Date(paste(ss[i], "-01", sep = ""))

  #季度初之前客户购买记录
  temp <- table(dachu$买家昵称[dachu$下单时间 < data1])

  #季度内客户购买记录
  if(i < length(ss)-2){
    data2 = as.Date(paste(ss[i+3], "-01", sep = ""))
    temp2 <- table(dachu$买家昵称[(dachu$下单时间 >= data1)&(dachu$下单时间 < data2)])
  }else{
    temp2 <- table(dachu$买家昵称[dachu$下单时间 >= data1])
  }

  #季度内回购记录
  temp2 = merge(data.frame(k=names(temp)[temp==1]),
                data.frame(k=names(temp2)),
                by=c(‘k‘))

  #保存日期、季度初新客数、月内回购数
  new_customer = rbind(new_customer, data.frame(date=ss[i], counts=sum(temp==1), repurchase=nrow(temp2)))

}
#计算回购率
new_customer$rate <- new_customer[[3]] / new_customer[[2]]
#colnames(new_customer) = c(‘date‘,‘counts‘,‘repurchase‘,‘rate‘)

win.graph()
opar<-par(mfrow=c(2,2))
plot(new_customer$date,new_customer$counts)
plot(new_customer$date,new_customer$repurchase);plot(new_customer$date,new_customer$rate)
par(opar)

write.csv(new_customer,"C:\\Users\\hasee\\Desktop\\new_customer.csv")

###************************************ 客户连带率:该段代码貌似有问题 ***********************************###
# 只针对所有一次客户
# 月连带率=本月发生连带的客户数/本月成交总客户数
# 产品连带率=购买该产品连带的客户数/购买该产品总体客户数
# 成交总客户=1次多件客户+一次一件客户
#数据导入
library(readxl)
# dachu <- read.csv("C:\\Users\\hasee\\Desktop\\liandailv.xlsx", header = T, encoding = "utf-8", colClasses = c("character", "Date", "character"))
# read_excel(path, sheet = 1, col_names = TRUE, col_types = NULL, na = "", skip = 0)
dachu <- read_excel("C:\\Users\\hasee\\Desktop\\liandailv.xlsx", sheet = 1, col_names = TRUE, col_types = c("text", "text", "text"), na = "", skip = 0)
dachu$下单日期 <- as.Date(dachu$下单日期)
str(dachu)
unique(dachu$商品ID)

#定义保存月度连带率
min(dachu$下单日期)
max(dachu$下单日期)
month_set=sort(unique(substr(dachu$下单日期,1,7)))

#月度连带率
month_associate_rate = data.frame()
date1 = min(dachu$下单日期)
for(i in seq(length(month_set))){
  if(i < length(month_set)){
    date2 = as.Date(paste(month_set[i+1], "-01", sep = ""))
    temp <- table(dachu$买家昵称[(dachu$下单日期 >= date1)&(dachu$下单日期 < date2)])
    date1 = date2
  }else{
    temp = table(dachu$买家昵称[dachu$下单日期 >= date1])
  }
  month_associate_rate = rbind(month_associate_rate, data.frame(month=month_set[i], count = length(temp), count2= sum(temp>1), rate=(sum(temp>1)/length(temp))))
}
month_associate_rate

#产品连带率
dachu$flag <- 0
head(dachu)
temp = table(dachu$买家昵称)
# library(dplyr)
# temp2 = left_join(dachu, data.frame(x = names(temp)[temp>1], flag.y = 1), by= c("买家昵称" = "x"),suffix = c("", ".y"))
temp2 = merge(dachu, data.frame(x = names(temp)[temp>1], flag.x = 1), by.x = "买家昵称", by.y = "x", all.x = TRUE)
temp2$flag[temp2$flag.x==1] = 1
temp2$flag.x = NULL
temp2

#定义保存产品连带率
prod_set=unique(dachu$商品ID)
product_associate_rate = data.frame()

#产品连带率
for(pi in prod_set){
  temp <- temp2$flag[temp2$商品ID == pi]
  product_associate_rate = rbind(product_associate_rate, data.frame(product=pi, count = length(temp), count2= sum(temp==1), rate=(sum(temp==1)/length(temp))))

}

product_associate_rate = product_associate_rate[order(product_associate_rate$count, decreasing = TRUE),]
product_associate_rate$product = as.character(product_associate_rate$product)
head(product_associate_rate)  

#验证
dachu[dachu$买家昵称 %in% dachu[dachu$商品ID=="42303520877",]$买家昵称,]

#产品连带率前五月度变化
#temp2为产品连带率里计算的那个
prod_set = product_associate_rate$product[1:5]
product_associate_rate_top5 = data.frame()
date1 = min(temp2$下单日期)
for(i in seq(length(month_set))){
  if(i < length(month_set)){
    date2 = as.Date(paste(month_set[i+1], "-01", sep = ""))
    temp <- temp2[(temp2$下单日期 >= date1)&(temp2$下单日期 < date2),]
    date1 = date2
  }else{
    temp = temp2[temp2$下单日期 >= date1,]
  }

  temp3 = data.frame(month=month_set[i])
  for(pi in prod_set){
    temp4 = temp$flag[temp$商品ID==pi]
    temp3 = cbind(temp3, length(temp4), sum(temp4==1), ifelse(length(temp4)==0,0,sum(temp4==1)/length(temp4)))
  }

  product_associate_rate_top5 = rbind(product_associate_rate_top5, temp3)
}
colnames(product_associate_rate_top5)[-1] <- paste(‘top‘,rep(1:5,each=3),c(‘count‘,‘count2‘,‘rate‘),sep = ‘‘)
product_associate_rate_top5

#图形展示
win.graph()
opar<-par(mfrow=c(1,2))
plot(month_associate_rate$month, month_associate_rate$rate, type="l", col = "blue", main = "月度连带率", xlab = "月份", ylab="连带率")
plot(product_associate_rate$rate, main = "产品连带率", xlab = "产品", ylab="连带率")
par(opar)

write.csv(month_associate_rate,"C:\\Users\\hasee\\Desktop\\month_associate_rate.csv")
write.csv(product_associate_rate,"C:\\Users\\hasee\\Desktop\\product_associate_rate.csv") #, quote = TRUE
write.csv(product_associate_rate_top5,"C:\\Users\\hasee\\Desktop\\product_associate_rate_top5.csv") #, quote = TRUE

# dplyr包包含了各种关联查询的函数,如inner_join,left_join,full_join,rigth_join......
library(dplyr)
library("nycflights13")
# Drop unimportant variables so it‘s easier to understand the join results.
flights2 <-
  flights %>%
  select(year:day,tailnum, carrier)
flights2 %>%
  left_join(airlines,by= "carrier")

#merge(data.frame(x=1:3,y=0,z=2),data.frame(x=2:3,y=1:2),by=c("x"),all.x = T)

###******************************************* 回购率与首次消费金额关系 ********************************************###
dachu <- read.csv("D:\\Dasktop\\bigdata_game\\天池\\大厨\\suoyoukehushuju.csv", header = T, encoding = "utf-8", colClasses = c("character", "Date", "numeric"))
str(dachu)
head(dachu,20)

library(dplyr)
temp=head(dachu,20)
temp = head(arrange(dachu, 买家昵称, desc(下单时间)), 100);temp
#flights[order(flights$year, flights$month, flights$day), ]
#flights[order(desc(flights$arr_delay)), ]
#filter(group_by(temp, 买家昵称))

temp <- dachu%>%
  arrange(买家昵称, 下单时间) %>%
  group_by(买家昵称)%>%
  mutate(count = n())%>%
  slice(1)%>%
  filter()

win.graph()
opar<-par(mfrow=c(1,2))
#实付金额——购买次数分布图
plot(temp$实付金额, temp$count)
#实付金额——频数(人次)分布图
plot(table(temp$实付金额))
par(opar)

#通过第一个图,暂且分组0-1000等距每200,1000-2000,2000以上
temp$group <- 0
temp[temp$实付金额 < 1000, ]$group <- temp[temp$实付金额 < 1000, ]$实付金额 %/% 100
temp[(temp$实付金额 >= 1000) & (temp$实付金额 < 2000), ]$group <- 10
temp[temp$实付金额 >= 2000, ]$group <- 11
head(temp,20)
temp2 <- temp%>%
  group_by(group)%>%
  summarise(n1=sum(count>1), n2=n(), rate = n1/n2)

win.graph()
#各组回购率分布图
plot(temp2$group, temp2$rate)

# i <- c("gamma","a")
# switch(i,
#        beta = "You typed beta",
#        alpha = "You typed alpha",
#        gamma = "You typed gamma",
#        delta = "You typed delta"
# )

###******************************************* 客户联带对回购的影响 *******************************************###
t0 <- Sys.time()
dachu <- read.csv("D:\\Dasktop\\bigdata_game\\天池\\大厨\\AnalysisOrderDownLoad-订单信息-子订单(全量)-10027396-8025-107.csv",
                  header = T, encoding = "utf-8", colClasses = c(rep("character",4), rep("Date",3), rep("character",5), "integer","numeric","character",rep("numeric",2)))
str(dachu)
dachu <- dachu[,4:5]
head(dachu)
dachu$买家昵称 <- substr(dachu$买家昵称,3,nchar(dachu$买家昵称)-1)
head(dachu,20)

library(dplyr)
#首单购买件数回购率
temp <- dachu %>%
  group_by(买家昵称, 下单时间) %>%
  summarise(count=n()) %>%
  arrange(买家昵称, 下单时间) %>%
  group_by(买家昵称) %>%
  mutate(count2=n()) %>%
  slice(1) %>%
  group_by(count) %>%
  mutate(n1 = n(), n2 = sum(count2>1), rate = n2/n1) %>%
  slice(1) %>%
  select(count, n1, n2, rate)

temp

win.graph()
plot(temp$count, temp$rate, main="首单购买件数与回购率", xlab = "首单购买件数",
     ylab = "回购客户占比", col="red")

#按月计算新客中回购客户占比
temp <- dachu %>%
  group_by(买家昵称, 下单时间) %>%
  summarise(count=n()) %>%  #连带件数
  mutate(year=as.integer(substr(下单时间,1,4)),
         month=as.integer(substr(下单时间,6,7))) %>%
  arrange(买家昵称, 下单时间) %>%
  group_by(买家昵称) %>%
  mutate(count2=n()) %>%  #回购次数
  slice(1) %>%  #第一次出现(前面的按时间排序不可少)即为新客
  group_by(year, month) %>%
  mutate(n1 = n(), n2 = sum(count>1), rate = n2/n1) %>%
  slice(1) %>%
  select(下单时间, year, month, n1, n2, rate)

temp
win.graph()
time <- seq.Date(as.Date(paste(substr(min(temp$下单时间),1,7), "-01", sep = "")),
                 by = "month", length = nrow(temp))
plot(time, temp$rate, main = "各月新客中连带客户占比", xlab = "月份",
     ylab = "首单购买多件客户占比", type = "l")

#按订单统计连带率(即购买多件订单与总订单之比)
temp <- dachu %>%
  group_by(买家昵称, 下单时间) %>%
  summarise(count=n())

sum(temp$count>1)/nrow(temp)

Sys.time()-t0

###############################################################################################################
#setwd("H:/数据分析/内部数据/薏凡特旗舰店数据/旗舰店客户数据分析/0803")
setwd("D:\\Dasktop\\bigdata_game\\天池\\大厨")
dat <- read.csv("kehushuju.csv",header=TRUE,encoding="utf-8",colClasses=c("character","Date","integer","numeric","integer"),stringsAsFactors = F)
dat <- arrange(dat, 买家昵称, 下单日期)
head(dat)
# new_dat<-unique(dat)  #数据量多时,计算量很大,而且基本不会出现重复记录,所以可以省略
# head(new_dat)
library(dplyr)

##回购次数与回购概率
###
temp <- dat %>%
  group_by(买家昵称)%>%
  summarise(count=n())
head(temp)
rr1 <- c()
rr2 <- c()
rate <- c()
max_count <- max(temp$count)
for (i in 1:(max_count-1)){    ###可能会出错,rate分母=0
  # rr1[i] <- summarise(filter(temp,count==i+1),n())
  # rr2[i] <- summarise(filter(temp,count>=i),n())
  # rate[i] <- summarise(filter(temp,count==i+1),n())/summarise(filter(temp,count>=i),n())
  rr1 <- c(rr1, sum(temp$count == i+1))  #效率更高
  rr2 <- c(rr2, sum(temp$count >= i))
  rate <- c(rate, rr1[i]/rr2[i])  #避免重复计算
}
temp2<-filter(temp,count>=2)
head(temp2)
rrr<-cbind(rr1,rr2,rate)

rrr
# write.csv(rrr,"H:/数据分析/内部数据/薏凡特旗舰店数据/旗舰店客户数据分析/0803/rrr.csv")

#计算回购周期#####

#添加购买次数列

new_dat2 <- select(dat, 买家昵称,下单日期, 下单时点)
# new_dat2<-data.frame(new_dat2)   #已经是数据框结构,而且即便转换格式此处也不对,应该为:new_dat2<-as.data.frame(new_dat2)
# new_dat2<-unique(new_dat2)
# head(new_dat2)

# temp2<-group_by(new_dat2,买家昵称)
# temp2<-summarise(temp2,count=n())
# temp2 <- new_dat2 %>%
#   group_by(买家昵称) %>%
#   summarise(count=n())
# head(temp2)
# count2<-unique(temp2$count)
#
# new_dat2$counts=0
# for(i in count2){
#   rg<-temp[temp2$count==i,]$买家昵称;
#   new_dat2[new_dat2$买家昵称 %in% rg,]$counts=i
#
# }

new_dat2 <- merge(new_dat2, temp, by=c(‘买家昵称‘))

head(new_dat2)
# old_dat<-filter(new_dat2,counts>=2)
# old_dat<-arrange(old_dat,下单日期)
# old_dat <- new_dat2 %>%    ##此处太慢,后面给出改进方法
#   filter(count>=2) %>%
#   arrange(下单日期)
# # old_dat<-unique(old_dat)
# head(old_dat)
# #max_count2<-max(old_dat$counts)
# #num<-c(1:max_count2)
# rebuy<-c()
# redays<-c()
# # t=1
# for(i in unique(old_dat$买家昵称) ){
#   rg<-filter(old_dat,old_dat$买家昵称==i)
#
#   for(j in 1:(rg$count[1]-1))
#   {
#     #t_diff <- rg$下单日期[j+1] - rg$下单日期[j]
#     t_diff <- as.integer(rg$下单日期[j+1] - rg$下单日期[j])
#     # rebuy[t]=j+1
#     # redays[t]=t_diff
#     # t=t+1
#     rebuy = c(rebuy,j+1)
#     redays = c(redays,t_diff)
#   }
# }
#
# head(rebuy)
# head(redays)
# mydata<-data.frame(rebuy,redays)
# #write.csv(mydata,"H:/数据分析/内部数据/薏凡特旗舰店数据/旗舰店客户数据分析/0803/mydata.csv")
# head(mydata)

###各时点回购人数占比
#不考虑时间因素时
rate <- data.frame()
for(i in sort(unique(dat$下单时点))){
  temp2 = new_dat2[new_dat2$下单时点 == i,]$count
  rate = rbind(rate, c(i, sum(temp2>1)/length(temp2)))
}
colnames(rate) <- c("下单时点", "rate")
rate

#考虑时间因素时
###如果考虑时间因素,则需加以下代码
new_dat3 <- arrange(new_dat2, 买家昵称, 下单日期)  #最好加排序,防止出错
head(new_dat3, 50)
# for(i in temp$买家昵称){  #由于循环较大故运行时间较长
#   new_dat3[new_dat3$买家昵称 == i,]$count <- 1:(temp[temp$买家昵称 == i,]$count)
# }
# head(new_dat3, 50)

#改进后,此方法必须对数据先排序!!
# t0 <- Sys.time()
# i <- 1; nmax <- nrow(new_dat3)
# repeat{
#   #m = i
#   n = new_dat3[i,4]
#   #ss = new_dat3[i,1]
#   # repeat{
#   #   i <- i + 1
#   #   if((new_dat3[i,1] != ss) | (i > nmax)){
#   #     new_dat3[m:(i-1),4] <- 1:new_dat3[m,4]
#   #     break
#   #   }
#   # }
#   new_dat3[i:(i + n - 1),4] <- 1:n
#   i = i+n
#   if(i > nmax)  break
# }
# Sys.time()-t0
#
# t0 <- Sys.time()
# i <- 1; nmax <- nrow(new_dat3)
# while(i <= nmax){
#   #m = i
#   n = new_dat3[i,4]
#   #ss = new_dat3[i,1]
#   # repeat{
#   #   i <- i + 1
#   #   if((new_dat3[i,1] != ss) | (i > nmax)){
#   #     new_dat3[m:(i-1),4] <- 1:new_dat3[m,4]
#   #     break
#   #   }
#   # }
#   new_dat3[i:(i + n - 1),4] <- 1:n
#   i = i+n
# }
# Sys.time()-t0

t0 <- Sys.time()
for(i in sort(unique(temp$count))){  #必须加sort排序
  df = (new_dat3$count == i)
  new_dat3[df, 4] <- rep(1:i, sum(df)/i)
}
Sys.time()-t0
head(new_dat3, 50)
tail(new_dat3,50)

#计算
rate2 <- data.frame(下单时点=c(), rate=c())
for(i in sort(unique(dat$下单时点))){
  temp2 = new_dat3[new_dat3$下单时点 == i,]$count
  rate2 = rbind(rate2, c(i, sum(temp2>1)/length(temp2)))
}
colnames(rate2) <- c("下单时点", "rate")
rate2

#改进方法
new_dat3$t_diff <- as.integer(new_dat3$下单日期 - c(new_dat3$下单日期[1], new_dat3$下单日期[-nrow(new_dat3)]))
head(new_dat3)
new_dat3$t_diff[new_dat3$count==1] <- 0
mydata <- new_dat3 %>%
  select(count, t_diff) %>%
  filter(count > 1) %>%
  rename(rebuy = count, redays = t_diff)
head(mydata)

plot(mydata)

#各次购买5天内回购情况
new_dat3$m5 <- (new_dat3$t_diff <5)
new_dat3$m5[new_dat3$count == 1] <- 0

setwd("H:/数据分析/内部数据/薏凡特旗舰店数据/旗舰店客户数据分析/0803/自我研究")
dat<-read.csv("kehushuju.csv",header=T,encoding="utf-8",colClasses=c("character","Date","integer","numeric","integer"))
head(dat)
library(dplyr)
dat1<-arrange(dat,下单日期)
head(dat1)
m=5 #定义回购周期,M=5表示客户在5天内回购
counts<-c(rep(0,length(dat1[,2])))
t0<-Sys.time()
for(i in 1:length(dat1[,2])){
  t_run<-dat1[,2][i]+m
  goal_dat1<-filter(dat1,下单日期<=t_run)
  if(length(filter(goal_dat1,goal_dat1$买家昵称==dat1[,1][i])[,1])>=2){
    counts[i]<-1
  }
}
tt<-Sys.time()-t0
head(counts)
end_dat5<-cbind(dat1,counts)
write.csv(end_dat5,"H:/数据分析/内部数据/薏凡特旗舰店数据/旗舰店客户数据分析/0803/自我研究/end_dat5.csv")

  

原文地址:https://www.cnblogs.com/iupoint/p/9769362.html

时间: 2024-11-10 14:12:22

给大厨写的R数据分析代码的相关文章

山大泰克条屏写串口的核心代码(海宏原创,转载请注明)

山大泰克条屏写串口的核心代码,海宏原创,转载请注明. using System; using System.Collections.Generic; using System.Text; // using System.Runtime.InteropServices; using System.IO.Ports; using System.Windows.Forms; using iPublic; namespace sdLed { /// <summary> /// 用来连接LED的API.

【OpenStack头道鲜】春风十里,不如听君一席,康大厨私人课堂开讲啦!

文/康鹏 俗话说,不会写文章的厨师不是一个好程序员,作为一枚既能文(码字)又能武(搬砖)的好厨师,不放点大招,怎么对得起咱这张脸?在这最美人间四月天,小e特邀 eSDK 开发部敲得了一手好代码,烧得了一桌好菜,写得了一篇好文,实乃居家旅行杀人越货必备之选的康大厨--康鹏,精心烹饪一桌精美的 OpenStack 大餐,邀君共享! 春风十里,不如听君一席,OpenStack 私人课堂今日开课,每周一期,期期不落,总有一款适合你~ OpenStack概述 如今在全球开源云计算领域,已走过近六个年头的

不负春光不负卿,听康大厨讲讲OpenStack重要组件的那些事儿

文/康鹏          前言 又到春暖花开时,不负春光不负卿. 康大厨精心烹饪OpenStack大餐,即将上菜! OpenStack 私人课堂今日第二讲,每周一期,期期不落,总有一款适合你~ OpenStack重要组件的功能   Nova 首先Nova这个项目是提供计算服务,它主要是负责实例的生命周期管理,实例在这里面也可以理解成是一个个的虚拟机,当然也未必是虚拟机,也可能是Linux的Container,其实它就是一个计算资源的单位.同时Nova还提供基于RESTFul的API接口,很方便

写一手简洁的代码

如果有人问我,为什么开始写代码...我会毫不犹豫的告诉他,因为IDEA的代码五颜六色的,很漂亮!!是的,我热爱写代码的初衷就是因为代码好"漂亮".从第一次开始写代码,到现在已经快2年了,"漂亮"这个词一直是我对代码热爱的初衷,只不过曾经的"漂亮"只局限于代码的颜色与字体,而现在的"漂亮"已经涵盖了颜色,接口,注释,方法,类等等,总之我喜欢写代码,只是因为它"漂亮". 之前在学校的时候,我就被实验室的同学&q

写可测试的代码

任何一个软件都是可以测试.在某种意义上,用户的使用过程也就是一个软件测试的过程.可是这并不是我们今天要讲的可测试性.我们讲的可测试性指的是代码的可测试性,通俗点儿说就是是一串代码里包含的逻辑是不是可以被单元测试所覆盖.在这篇文章里我会从单元测试的基本概念开始引伸到如何写单元测试,如何写可单元测试的代码.文章里所有的例子都是C#写的,一来它是我职业生涯的主力语言.二来C#广为人知,相信对广大职业的或是业余的程序员来说读懂C#的代码不会是什么特别困难的事情.实际上我描述的方法和概念并不会局限于C#或

ASP.NET 大文件下载的实现思路及代码

文件下载是一个网站最基本的功能,ASP.NET网站的文件下载功能实现也很简单,但是如果遇到大文件的下载而不做特殊处理的话,那将会出现不可预料的后果.本文就基于ASP.NET提供大文件下载的实现思路及代码. 当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃.可以参考如下代码来避免这个问题. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

如何写出无法维护的代码

如何写出无法维护的代码 酷壳里有很多我觉得很不错的文章,但是访问量最大的却是那篇<6个变态的Hello World>,和它能在本站右边栏“全站热门”中出现的还有“如何加密源代码”,以及编程真难啊等这样的文章.可见本站的读者们的偏好,我也相信你们都是“身怀绝技”的程序员.所以,今天给大家推荐这篇文章,相信一定能触动大家的兴奋点. 这篇文章的原文在这里(http://mindprod.com/jgloss/unmain.html),我看完后我想说—— 什么叫“创造力”,创造力就是——就算是要干一件

如何写出优美的代码——小体会1

1.不要把业务逻辑和非业务逻辑混合 多思考一下,尽量不要有错误的逻辑,和漏掉的情形 常见的逻辑: (1)字符串的为空判断 if(s == null || s.length() <= 0); 如果 String 类型为null, 而去进行 equals(String) 或 length() 等操作会抛出java.lang.NullPointerException. (2)对象是否为空判断 public static boolean isNullOrEmpty(Object obj) { if (o

不要把时间浪费在写出完美的代码

一个系统可能会持续工作5年,10年,20年甚至更长的时间.但是具体到这个系统中的某一行代码,即使是关于设计的部分,这一行代码存在的时间却会很短:几个月或者几天,甚至是几分钟. 一些代码比其他代码更重要 通过研究代码是怎么随时间改变的,Michael Feathers定义了一条代码变动曲线.每个系统都有很多写完之后就不再改变的代码.与此同时,也存在少量这样的代码,这些代码是整个系统最重要也是最有用的代码,它们会随时间一次又一次地改变.重构,或者被删除,重新来过,如是反复几次. 随着你对一个系统越来