• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Jun 19th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. library(dplyr)
2.
3. effective.costs <- data.frame(
4.   user = c(rep("User1", 3), rep("User2", 3)),
5.   month = c(rep(c("2019-01", "2019-02", "2019-03"), 2)),
6.   current_subscription = c(rep("subscription_1", 3), rep("subscription_2", 3)),
7.   costs = c(70, 20, 50, 150, 130, 170)
8. )
9.
10. predicted.costs <- data.frame(
11.   user = c(rep("User1", 9), rep("User2", 9)),
12.   month = c(rep("2019-01",3), rep("2019-02", 3), rep("2019-03", 3)),
13.   subscription = c(rep(c("subscription_1", "subscription_2", "subscription_3"), 6)),
14.   calculated_costs = c(
15.     c(70, 50, 110, 20, 50, 70, 50, 80, 120),
16.     c(190, 150, 110, 210, 130, 110, 250, 170, 110)
17.     )
18. )
19.
20. comparison <- merge(effective.costs, predicted.costs, by = c("user", "month"))
21.
22. getRecommendation <- function(x) {
23.   subscription <- predicted.costs %>%
24.     filter(
25.       calculated_costs < x['costs'] &
26.       user == x['user'] &
27.       month == x['month']
28.     ) %>%
29.     arrange(calculated_costs) %>%
30.     select(subscription)
31.   subscription <- ifelse(
32.     length(subscription) > 0,
33.     as.character(subscription[1, 1]),
34.     NA
35.   )
36.   # I know return is not needed, but I'm used to it... :-)
37.   return(subscription)
38. }
39.
40. effective.costs\$recommendation <- apply(effective.costs, 1, getRecommendation)
41.
42. View(effective.costs)
43.
44. getRecommendation <- function(x) {
45.   subscription <- predicted.costs %>%
46.     filter(
47.       calculated_costs < x['costs'] &
48.       user == x['user'] &
49.       month == x['month']
50.     ) %>%
51.     arrange(calculated_costs) %>%
52.     select(subscription)
53.   subscription <- ifelse(
54.     length(subscription) > 0,
55.     as.character(subscription[1, 1]),
56.     NA
57.   )
58.   # I know return is not needed, but I'm used to it... :-)
59.   return(subscription)
60. }
61.
62. effective.costs\$recommendation <- apply(effective.costs, 1, getRecommendation)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top