I have a zoo object with three columns. I want to calculate pairwise differences for each column to get the relative change over time. I'm using a for-loop for this. However, what I get returned isn't a zoo object anymore but a normal data.frame.
When I want to plot it in multiple panels, I loose my x-axis time labels. When I try to transform the dataframe back into a zoo object, I get an error of "bad entries"
head(zoo.2017)
zoo1 zoo2 zoo3
2017-01-01 104.6066 106.6240 112.7336
2017-01-02 103.1034 104.4088 111.8473
2017-01-03 103.4550 107.0606 111.4292
2017-01-04 101.6916 108.0550 111.8183
2017-01-05 101.8274 105.1528 114.3505
2017-01-06 102.9502 107.1381 113.8313
delta<-zoo()
for(n in 2:length(zoo.2017[,1])){
diff<-as.data.frame(zoo.2017[n,])-as.data.frame(zoo.2017[n-1,])
delta<-rbind(delta, diff)
}
head(delta)
Maui6 Olowalu Pali
diff -1.503264 -2.215139 -0.8862847
2017-01-03 0.3516667 2.651771 -0.4180556
2017-01-04 -1.763472 0.9944444 0.3890625
2017-01-05 0.1358681 -2.902257 2.532153
2017-01-06 1.122743 1.985313 -0.5191319
2017-01-07 1.430937 0.5648958 -0.01836806
I know the error probably comes from the as.data.frame() in the for loop, but I don't know how else to compute it. When I try an read.zoo() on the dataframe afterwards, it gives me the bad entries error...
I'm still having a hard time with zoo
dput(head(zoo.2017))- ngmdiff(zoo.2017)just give you what you want anyway? - ngm