2016-11-05 21 views
0

我在处理趋势时陷入困境。我的问题类似于下面的线程,但我有一个额外的变量称为'项目'。确定具有多个变量(如客户ID /项目等)的销售趋势

How to determine trend of time-series of values in R

我的最终结果将是象下面这样的样品。请帮助

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

我的数据:

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")] 
+0

除了data.table和“通过”键询问多次,这具有强相似/是[如何确定R中一系列值的趋势]的可能重复(http://stackoverflow.com/questions/23600385/how-to-determine-trend-of-a-series-of-values -in-R)。这些是一些标准的例子还是作业? – smci

+0

有关如何[如何将data.table分组为多个列?](https://stackoverflow.com/questions/12478943/r-data-table-group-by-multiple-columns)的部分是使用'by = list(“customer_ID”,“item”)',而不是'by = c(...)' – smci

+0

@smci - 您的解决方案不起作用。错误:[.data.table'中的错误(数据,列表(N.Minus.1 = .N-1,Change = list(sales [transaction_num +: 'by'或'keyby'列表中的项目是长度(1,1),每个长度必须与x中的行或i(20)返回的行数相同。 – Murali

回答

1

和我概括是data.table方法:

require(data.table) 

trend <- function(x) { 
    ifelse(all(diff(x)>0), 'Positive', 
    ifelse(all(diff(x)<0), 'Negative', 'Mixed')) 
} 

dat[, trend(sales), by=c("customer_ID","item")] 
    customer_ID item  V1 
1:  Josh milk Positive 
2:  Josh eggs Negative 
3:   Ray milk Mixed 
4:   Ray eggs Positive 
5:  Eric milk Negative 
6:  Eric eggs Mixed 

# or if you want to assign the result... 
dat[, Sales_Slope:=trend(sales), by=c("customer_ID","item")] 
1

我与@smci同意,所有从该链接改变的是“通过”可变有所增加。我希望这个解决方案可以清楚

> 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 
+0

谢谢你们俩。解决方案的工作就像魅力:-)非常感谢,并感谢您对社区的贡献。我们喜欢R – Murali

+0

很高兴我可以帮助! –

+0

嵌套的'ifelse(...)'是一个更好的更清洁的成语,它也允许你把它写成一个表达式而不用''return'语句混淆它​​。参见我的a nswer。 – smci