将功能写入子集data.frame

时间:2018-09-29 14:53:49

标签: r

我需要找到每个城市的前十名客户,按不同产品和不同时间段的还款额计算。该功能应能够获取产品(金/银/铂)和时间段(每年或每月),并且应该返回前10位客户。

 head(Repayment,20)


 Customer      Month    Amount  Limit  Product      City
1        A1 2004-01-12 495414.75 500000     Gold BANGALORE
2        A1 2004-01-03 245899.02 500000     Gold BANGALORE
3        A1 2004-01-15 259490.06 500000     Gold BANGALORE
4        A1 2004-01-25 437555.12 500000     Gold BANGALORE
5        A1 2005-01-17 165972.88 500000     Gold BANGALORE
6        A1 2005-02-23 365366.62 500000     Gold BANGALORE
7        A1 2005-02-01  14473.41 500000     Gold BANGALORE
8        A8 2004-02-05 100002.00 100002   Silver     PATNA
9        A9 2005-02-28 100003.00 100003   Silver   CHENNAI
10      A10 2005-02-16  48759.19 500000     Gold  CALCUTTA
11      A11 2005-02-23 208317.94 500000     Gold    COCHIN
12      A12 2005-02-03 150281.43 500000     Gold    BOMBAY
13      A13 2006-02-04 339078.03 500000     Gold BANGALORE
14      A14 2006-03-25 137834.50 500000     Gold BANGALORE
15      A15 2006-03-31 437119.51 500000     Gold  CALCUTTA
16      A16 2006-03-23 103923.70 500000     Gold    COCHIN
17      A17 2004-03-19 408466.94 500000     Gold    BOMBAY
18      A18 2006-03-05 100000.00 100000   Silver BANGALORE
19      A19 2005-04-04  10000.00  10000 Platinum BANGALORE
20      A20 2006-04-30  10001.00  10001 Platinum  CALCUTTA
> 

我的代码尝试:我对此感到有些困惑。我尝试了if语句是否在函数内部。

if(time_period== "monthly"){

   top1 <- group_by(Repayment, product=product_name, month(Month, Customer, City)) %>% 
           summarise(repay = Sum(Amount)) 

   top1 <- arrange(top1, desc(repay)) 

   return top1[1:10,1] 

} else { 

   top2 <- group_by(Repayment, product=product_name, year(Month), Customer, City) %>% 
           summarise(repay = Sum(Amount)) 

   top2 <- arrange(top2, desc(repay)) 

   return top2[1:10,1]
 }

0 个答案:

没有答案