Murali - 1 year ago 62
R Question

Determine trend of a data.table series, group by multiple variables

I got stuck while working on trends. My problem is similar to this below thread but I have one extra variable called 'item'.

how to determine trend of a series of values in R

``````Customer_ID Item    Sales_Slope
Josh        milk      Positive
Josh         eggs      Negative
Eric         milk      Mixed
Eric         eggs      postive
``````

My Data:

``````require("data.table")
dat <- data.table(
customer_ID=c(rep("Josh",6),rep("Ray",7),rep("Eric",7)),
item=c(rep("milk",3),rep("eggs",3),rep("milk",4),rep("eggs",3),rep("milk",3),rep("eggs",4)),
sales=c(35,50,65,65,52,49,15,10,13,9,35,50,65,65,52,49,15,10,13,9))

dat[,transaction_num:=seq(1,.N), by=c("customer_ID")]
``````

I do agree with @smci that all that changed from that link is that "by" variable has increased. I hope this solution makes it clear

``````> library(plyr)
> abc <- function(x){
if(all(diff(x\$sales)>0)) return('Positive')
if(all(diff(x\$sales)<0)) return('Negative')
return('Mixed')
}

y= ddply(dat, .(customer_ID, item), abc)
y
customer_ID item       V1
1        Eric eggs    Mixed
2        Eric milk Negative
3        Josh eggs Negative
4        Josh milk Positive
5         Ray eggs Positive
6         Ray milk    Mixed
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download