问题
I have the below data frame where I am trying to merge multiple transactions of the one customer into one single record.
Input:
ST_DATE ND_DATE LO_NO ACTV_CODE ACTV_AMT AB_NO FEATURE_CODE L_NU
7/27/16 7/27/16 265 O 15 1 INTEREST 855
7/27/16 7/27/16 265 O 14 1 INSTALLMENT 855
Expected Output:
ST_DATE ND_DATE LO_NO ACTV_CODE ACTV_AMT AB_NO FEATURE_INTEREST FEATURE_INSTALLMENT L_NU
7/27/16 7/27/16 265 O 29 1 1 1 855
Tried:
install1 <- install %>%
group_by(LO_NO,AB_NO,L_NU) %>%
slice(which.min(as.Date(ST_DATE, '%Y/%m/%d'))) %>%
slice(which.max(as.Date(ND_DATE, '%Y/%m/%d'))) %>%
summarise(ACTV_AMT = sum(ACTV_AMT)) %>%
spread(FEATURE_CODE,fill = 0) %>% # confused here on what to put key value pairs so that I can get the above output
Can anyone help me in implementing the code. There are many feature_codes
are available if that feature code is not associated with the customer it should fill 0
or if there are 2 same feature_codes
like INTEREST is twice for that month it should fill as 2
in FEATURE_INTEREST
回答1:
You can first use group by and then use dcast to spread out the aggregated values-
install1 <- install %>%
group_by(LO_NO,AB_NO,L_NU,FEATURE_CODE) %>%
summarise(count1= n())
library(reshape2)
final <- dcast(install1, formula = LO_NO + AB_NO + L_NU ~ FEATURE_CODE, fill = 0)
Hope this helps
来源:https://stackoverflow.com/questions/47115780/spread-and-merge-row-records-in-r-for-the-same-customer