Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(dplyr)
- effective.costs <- data.frame(
- user = c(rep("User1", 3), rep("User2", 3)),
- month = c(rep(c("2019-01", "2019-02", "2019-03"), 2)),
- current_subscription = c(rep("subscription_1", 3), rep("subscription_2", 3)),
- costs = c(70, 20, 50, 150, 130, 170)
- )
- predicted.costs <- data.frame(
- user = c(rep("User1", 9), rep("User2", 9)),
- month = c(rep("2019-01",3), rep("2019-02", 3), rep("2019-03", 3)),
- subscription = c(rep(c("subscription_1", "subscription_2", "subscription_3"), 6)),
- calculated_costs = c(
- c(70, 50, 110, 20, 50, 70, 50, 80, 120),
- c(190, 150, 110, 210, 130, 110, 250, 170, 110)
- )
- )
- comparison <- merge(effective.costs, predicted.costs, by = c("user", "month"))
- getRecommendation <- function(x) {
- subscription <- predicted.costs %>%
- filter(
- calculated_costs < x['costs'] &
- user == x['user'] &
- month == x['month']
- ) %>%
- arrange(calculated_costs) %>%
- select(subscription)
- subscription <- ifelse(
- length(subscription) > 0,
- as.character(subscription[1, 1]),
- NA
- )
- # I know return is not needed, but I'm used to it... :-)
- return(subscription)
- }
- effective.costs$recommendation <- apply(effective.costs, 1, getRecommendation)
- View(effective.costs)
- getRecommendation <- function(x) {
- subscription <- predicted.costs %>%
- filter(
- calculated_costs < x['costs'] &
- user == x['user'] &
- month == x['month']
- ) %>%
- arrange(calculated_costs) %>%
- select(subscription)
- subscription <- ifelse(
- length(subscription) > 0,
- as.character(subscription[1, 1]),
- NA
- )
- # I know return is not needed, but I'm used to it... :-)
- return(subscription)
- }
- effective.costs$recommendation <- apply(effective.costs, 1, getRecommendation)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement