Murali 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

My end result would be like below sample. Please help

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

My Data:

dat <- data.table(

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

Answer Source

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')

 y= ddply(dat, .(customer_ID, item), abc)
  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