2013-10-24 39 views
0

我有一個時間序列數據已被導入爲動物園類。我的數據是這樣的:R只使用差值獲得正/負三角形

# you can recreate my problem using the code below 
library(rjson) 
library(plyr) 
library(zoo) 
value <- '[["2013-08-08", 7944, 0.37], ["2013-08-09", 7924, 0.37], ["2013-08-09", 7924, 0.37], ["2013-08-10", 7895, 0.37], ["2013-08-10", 7895, 0.37], ["2013-08-11", 7895, 0.37], ["2013-08-12", 7895, 0.37], ["2013-08-12", 7895, 0.37], ["2013-08-13", 8087, 0.37], ["2013-08-13", 8087, 0.37], ["2013-08-14", 8081, 0.37], ["2013-08-14", 8081, 0.37], ["2013-08-15", 8016, 0.37], ["2013-08-15", 8016, 0.37], ["2013-08-16", 7991, 0.37], ["2013-08-16", 7991, 0.37], ["2013-08-17", 7969, 0.37], ["2013-08-17", 7969, 0.37], ["2013-08-18", 7969, 0.37], ["2013-08-18", 7969, 0.37], ["2013-08-19", 7969, 0.37], ["2013-08-19", 7969, 0.37], ["2013-08-20", 3931, 0.37], ["2013-08-20", 3931, 0.37], ["2013-08-21", 3829, 0.37], ["2013-08-21", 3829, 0.37], ["2013-08-22", 3729, 0.37], ["2013-08-22", 3729, 0.37], ["2013-08-23", 3729, 0.37], ["2013-08-23", 3729, 0.37], ["2013-08-24", 3719, 0.37], ["2013-08-24", 3719, 0.37], ["2013-08-25", 3719, 0.37], ["2013-08-25", 3719, 0.37], ["2013-08-26", 3719, 0.37], ["2013-08-26", 3719, 0.37], ["2013-08-27", 7569, 0.37], ["2013-08-27", 7569, 0.37], ["2013-08-28", 7444, 0.37], ["2013-08-28", 7444, 0.37], ["2013-08-29", 7444, 0.37], ["2013-08-29", 7444, 0.37], ["2013-08-30", 7439, 0.37], ["2013-08-30", 7439, 0.37], ["2013-08-31", 7419, 0.37], ["2013-08-31", 7419, 0.37], ["2013-09-01", 7419, 0.37], ["2013-09-01", 7419, 0.37], ["2013-09-02", 7419, 0.37], ["2013-09-02", 7419, 0.37], ["2013-09-03", 7219, 0.37], ["2013-09-03", 7219, 0.37], ["2013-09-05", 7001, 0.37], ["2013-09-06", 6999, 0.37], ["2013-09-07", 2749, 0.37], ["2013-09-08", 2749, 0.37], ["2013-09-08", 2749, 0.37], ["2013-09-09", 2749, 0.37]]' 
content <- as.data.frame(matrix(unlist(fromJSON(json_str=.value)),ncol=3,byrow=TRUE)) 
names(content) <- c('date', 'inStock', 'unitPrice') 
content$date <- as.POSIXct(content$date, format="%Y-%m-%d") 
content$inStock <- as.integer(levels(content$inStock))[content$inStock] 
content$unitPrice <- as.numeric(levels(content$unitPrice))[content$unitPrice] 
content <- ddply(content, .(date), summarise, inventoryValue = max(inStock) * min(unitPrice), inStock = max(inStock), unitPrice = min(unitPrice)) 
content.zoo <- zoo(content[-1], content$date) 
content.complete <- na.approx(content.zoo, xout=seq(start(content.zoo), end(content.zoo), by="day")) 
plot(content.complete[,1]) 

enter image description here

你可以很容易地看到庫存下降的運動,1500在8月20日和存貨得到了在八月25

如何補充使用差異函數來計算銷售額,這只是負增量,或者將所有正增量(補貨)分配爲0

# the plot below shows all the delta but I only want the negative delta 
plot(-diff(content.complete[,1])) 

enter image description here

以簡單的方式。你有一個載體:

a <- c(1,2,3,4,-1,-2,4,5, 0) 
# how can I get 
c(1,2,3,4,0,0,4,5,0) 

UPDATE:

#Inspired by pmax answer, this is my final solution: 
plot(zoo(pmax(0, as.vector(-diff(content.complete[,1]))), seq(start(content.complete), end(content.complete), by="day"))) 
+0

這是我不清楚你在問什麼。你想過濾掉你的數據中的零嗎? –

+0

對不起..我只想要負三角洲但差異函數返回我每一個增量 –

回答

2

一種方法是使用pmax

> pmax(0,a) 
[1] 1 2 3 4 0 0 4 5 0 
+0

我總是忘記這個功能。非常方便 –

+0

非常感謝您的回答,它幫助了我。 –

0

鑑於我正確地理解你的問題:你不能使用which()功能,使

index <- which(-diff(content.complete[,1]) < 0.0) 

返回diff給出否定結果的指數?

a <- rnorm(10) 
[1] 0.79466221 -0.02602763 -0.08978353 2.66642150 0.10041665 -1.57871469 
[7] -0.33173812 -0.70527085 0.01021448 -0.43410244 

index <- which(diff(a) < 0) 
[1] 1 2 4 5 7 9