I have a question linked to: Cumsum excluding current value How to apply cumsum excluding a certain customer.id? For instance:
order.id customer.id Apples Peaches Pears
1001 J Car Ltd 1 0 0
1002 Som Comp 1 2 0
1005 Richardson 0 0 1
1004 J Car Ltd 1 0 0
1003 J Car Ltd 2 0 0
1006 Richardson 1 0 1
1007 Aldridge 0 0 1
1008 J Car Ltd 0 0 1
1010 Som Comp 0 1 0
I want to apply cumsum to keep track of previous apple orders:
Fruits <- Fruits[order(Fruits$order.id), ] #sort data
Fruits$prev_Apples<-with(Fruits,
ave(
ave(Apples, customer.id, FUN=cumsum), #get running sum per customer.id
interaction(customer.id, order.id, drop=T),
FUN=max, na.rm=T) #find largest sum per index per seg
)
But I also want to exclude from my cumsum customer.id Som Comp. For him, I want the prev_Apples column to be equal to 0:
order id customer id Apples Peaches Pears Prev_Apples
1001 J Car Ltd 1 0 0 0
1002 Som Comp **1** 2 0 0
1003 J Car Ltd 2 0 0 1
1004 J Car Ltd 1 0 0 3
1005 Richards 0 0 1 0
1006 Richards 1 0 1 0
1007 Aldridge 0 0 1 0
1008 J Car Ltd 0 0 1 4
1010 Som Comp 1 0 0 **0**
So I thought to add this line of code:
if(Fruits$customer id =='NULL'){
Prev_Apples = 0
return (Fruits$customer id)
}
But of course I get the error: “the condition has length > 1 and only the first element will be used”
I understood why I get the error, but how can I avoid it? Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire