You are on page 1of 3

setwd("C:\\Users\\LENOVO\\Desktop\\New folder")

rm(flights)

install.packages("dplyr")

install.packages("lubridate")

library("dplyr")

library("lubridate")

campaign <- read.table("Campaign_File.txt", header = TRUE)

View(campaign)

rm(campaign)

product <- read.table("Products_File.txt", header = TRUE, sep = "\t")

View(product)

transactions <- read.table("Transactions_File.txt", header = TRUE)

View(transactions)

rm(transactions)

transaction <- read.table("Transactions_File.txt", header = TRUE,sep = "\t")

View(transaction)

merged_product_trans <- merge(x=transaction,y=product, by="Product_Code", all.x = TRUE)

View(merged_product_trans)

gr_product_category <- group_by(merged_product_trans,Product_Category)

View(gr_product_category)

aggregate(merged_product_trans$Items_Amount, by=list(Product category =


merged_product_trans$Product_Category,function(sum)

tapply(merged_product_trans$Items_Amount,merged_product_trans$Product_Category,FUN = sum)

hist(merged_product_trans$Payment_Method)

install.packages("plyr")

library(plyr)
count(merged_product_trans$Payment_Method)

class(merged_product_trans$Timestamp)

merged_product_trans$Timestamp <- ymd_hms(merged_product_trans$Timestamp)

class(merged_product_trans$Timestamp)

transaction_18 <- merged_product_trans%>%filter(Timestamp==18)

with(merged_product_trans,merged_product_trans[hour(Timestamp)>=18:00])

customer <- read.table("Customers_File.txt", header = TRUE, sep = "\t")

View(customer)

merged_tran_cardID <- merge(x=transaction,y=customer,by="Card_ID",all.x = TRUE)

View(merged_tran_cardID)

removed_dupli <- merged_tran_cardID[-which(duplicated(merged_tran_cardID$Card_ID)),]

View(removed_dupli)

merged_tran_cardID$Date <- as.Date("2017-01-01")

View(merged_tran_cardID)

merged_tran_cardID$Birth_Date <- ymd(merged_tran_cardID$Birth_Date)

class(merged_tran_cardID$Date)

merged_tran_cardID$age_diff <-
time_length(difftime(merged_tran_cardID$Date,merged_tran_cardID$Birth_Date),"years")

merged_tran_cardID$Age_Grouping <- cut(merged_tran_cardID$age_diff,breaks = c(seq(25,115,


by=15)))

View(merged_tran_cardID)

tapply(merged_tran_cardID$Items_Amount,merged_tran_cardID$Age_Grouping,FUN = sum)

tapply(merged_tran_cardID$Items_Amount,list(merged_tran_cardID$Age_Grouping,
merged_tran_cardID$Gender),FUN = sum)

campaign <- read.table("Campaign_File.txt", header = TRUE, sep = "\t")

View(campaign)

names(campaign)
merge_Cust_Camp <- merge(x=customer,y=campaign,by= "Card_ID",all.x = TRUE)

View(merge_Cust_Camp)

True <- subset(merge_Cust_Camp,Campaign_Responce=="TRUE")

View(True)

True$Date <- as.Date("2004-12-31")

class(merge_Cust_Camp$Registration_Date)

True$Registration_Date <- ymd(True$Registration_Date)

class(merge_Cust_Camp$Date)

True$TimeDiff <- time_length(difftime(True$Date,True$Registration_Date),'years')

True$timegrouping <- cut(True$TimeDiff,breaks = c(seq(3,7.5, by=1.5)))

tapply(True$timegrouping,True$Campaign_Responce,FUN = count)

merge_Cust_Camp$Date <- as.Date("2004-12-31")

merge_Cust_Camp$TimeDiff <-
time_length(difftime(merge_Cust_Camp$Date,merge_Cust_Camp$Registration_Date),"years")

merge_Cust_Camp$timegrouping <- cut(merge_Cust_Camp$TimeDiff,breaks = c(seq(3,7.5, by=1.5)))

count(merge_Cust_Camp$timegrouping)