I am trying to make a user defined function with two inputs. and for input it has a condition i.e : The user should be able to specify the product (Gold/Silver/Platinum) and time period (Year or Months ) and the function should take this inputs while identifying the top 10 Customers.
Data frame
head(customer_repay,20)
Customer Age City Product Limit Company Segment Month Repayment Amount Year Months Amount
1 A1 76 BANGALORE Gold 500000 C1 Self Employed 2004-01-12 495414.75 2004 Jan 495414.75
2 A1 76 BANGALORE Gold 500000 C1 Self Employed 2004-01-03 245899.02 2004 Jan 245899.02
3 A1 76 BANGALORE Gold 500000 C1 Self Employed 2004-01-15 259490.06 2004 Jan 259490.06
4 A1 76 BANGALORE Gold 500000 C1 Self Employed 2004-01-25 437555.12 2004 Jan 437555.12
5 A1 76 BANGALORE Gold 500000 C1 Self Employed 2005-01-17 165972.88 2005 Jan 165972.88
6 A1 76 BANGALORE Gold 500000 C1 Self Employed 2005-02-23 365366.62 2005 Feb 365366.62
7 A1 76 BANGALORE Gold 500000 C1 Self Employed 2005-02-01 14473.41 2005 Feb 14473.41
8 A1 76 BANGALORE Gold 500000 C1 Self Employed 2004-01-12 350446.82 2004 Jan 350446.82
9 A1 76 BANGALORE Gold 500000 C1 Self Employed 2004-02-05 117964.43 2004 Feb 117964.43
10 A1 76 BANGALORE Gold 500000 C1 Self Employed 2006-04-19 79849.19 2006 Apr 79849.19
11 A1 76 BANGALORE Gold 500000 C1 Self Employed 2005-11-22 402099.78 2005 Nov 402099.78
12 A1 76 BANGALORE Gold 500000 C1 Self Employed 2006-11-21 169358.40 2006 Nov 169358.40
13 A1 76 BANGALORE Gold 500000 C1 Self Employed 2005-07-03 297176.74 2005 Jul 297176.74
14 A1 76 BANGALORE Gold 500000 C1 Self Employed 2006-09-03 186427.50 2006 Sep 186427.50
15 A1 76 BANGALORE Gold 500000 C1 Self Employed 2005-02-23 96670.70 2005 Feb 96670.70
16 A1 76 BANGALORE Gold 500000 C1 Self Employed 2006-04-01 429099.97 2006 Apr 429099.97
17 A1 76 BANGALORE Gold 500000 C1 Self Employed 2004-01-15 453027.64 2004 Jan 453027.64
18 A1 76 BANGALORE Gold 500000 C1 Self Employed 2005-02-16 187398.64 2005 Feb 187398.64
19 A2 71 CALCUTTA Silver 100000 C2 Salaried_MNC 2004-01-03 185955.07 2004 Jan 100000.00
20 A2 71 CALCUTTA Silver 100000 C2 Salaried_MNC 2005-02-28 412783.34 2005 Feb 100000.00
>
i have written this piece of code .
if (Prod %in% c('Gold','Silver','Platinum') & Time %in% c('Year','Months')) {
df_result<-customer_repay%>%group_by(Months,City,Product,Customer)%>%summarise(Repayment = sum(`Repayment Amount`))
View(df_result)
}else{
print('Enter the correct Parameter.')
}
CodePudding user response:
As a general starting point, you can use the code you've already developed to get to this point:
name_of_function <- function(
df, # data frame
Prod, # product
Time # time
)
{
# Require "magrittr" package for `%>%`
require(magrittr)
# Check if product and time are valid
if (Prod %in% c('Gold','Silver','Platinum') & Time %in% c('Year','Months')) {
df_result <- df %>%
dplyr::group_by(Months, City, Product, Customer) %>%
dplyr::summarise(Repayment = sum(`Repayment Amount`))
}else if(!Prod %in% c('Gold', 'Silver', 'Platinum')){
# Invalid product
stop(
"Product input into 'Prod' is not valid."
)
}else if(!Time %in% c('Year', 'Months')){
# Invalid time
stop(
"Time input into 'Time' is not valid."
)
}
# Top 10 customers (based on repayment)
df_result <- df_result[order(df_result$Repayment, decreasing = TRUE),]
result <- df_result[1:10,]
# Returns data frame
return(result)
}
# Use function with `customer_repay` data frame
name_of_function(
df = customer_repay,
Prod = "Gold",
Time = "Year"
)
The function assumes that you would like the top 10 customers based on their value of repayment.
