In: Computer Science
# FOR THE NEXT FEW QUESTIONS YOU WILL WORK WITH THE TEN SEGMENTS DATA FROM PART 1 OF THE EXAM. THE DATA ARE LOCATED ON Canvas.
10) Write code to import TenSegments.csv into R as a data frame tensegments
11) Write code that creates a new column with the CLV for all segments using formula 1. Assume a discount rate of 10%.
12) Write a for loop that loops through all the CLV values and creates a new vector that contains any CLV values that are negative
# FOR 1 BONUS POINT: # Write 1 line of code that calculates the average acquisition cost for the 3 segments with the highest CLV. You cannot hard code any values into your code.
STEP - BY - STEP PROCESS
(10.) Since I have no dataset, So I have created this one. You can import your csv dataset like this ==>
df <- read.csv("location of the csv file", header = TRUE)
Segment <- as.numeric(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
AcquisitionCost <- as.numeric(c(177.25, 37.49, 156.11, 171.23, 89.86,
135.26, 139.99, 129.07, 125.44, 173.22))
RetentionRate <- as.numeric(c(0.95, 0.5, 0.8, 0.85, 0.55, 0.7, 0.75, 0.65, 0.6, 0.9))
Margin <- as.numeric(c(1911.26, 715.83, -400.5, -292.51, -290.5,
2957.98, 2890.01, 2224.93, 1.65, 420.51))
df <- data.frame(Segment, AcquisitionCost, RetentionRate, Margin)
df
Output:
(11.) Write code that creates a new column with the CLV for all segments using formula 1. Assume a discount rate of 10%.
discount_rate = 0.10
df['CLV'] <- ((df$Margin * df$RetentionRate) / (1 + discount_rate - df$RetentionRate)) - df$AcquisitionCost
df
Output:
(12.) Write a for loop that loops through all the CLV values and creates a new vector that contains any CLV values that are negative.
negative_CLV <- c()
for (i in 1:length(df$CLV)) {
if (df$CLV[i] < 0) {
negative_CLV[i] <- df$CLV[i]
}
}
negative_CLV <- na.omit(negative_CLV)
for (i in 1:length(negative_CLV)) {
print(negative_CLV[i])
}
Output:
(13.) code that calculates the average acquisition cost for the 3 segments with the highest CLV.
df1 <- head(df[with(-df, order(CLV)), ], 3)
mean(df1$AcquisitionCost)
Output:
NOTE: Please use R-Studio for better understanding !!!
Thumbs Up Please !!!