I would like to know how to keep the row names when using the running()
function.
e.g. I'm applying a running correlation between two time series, and for me is very important keeping the row names because these are the Years of my time series.
This is an example:
library(gtools)
test <- matrix(rnorm(100, sd=4), nrow = 50, ncol=2,
dimnames= list(1966:2015, c("var1", "var2")))
# View(running(test[,1], test[,2], fun=cor, width=5, by=5))
run.test <- running(test[,1], test[,2], fun=cor, width=5, by=5)
and I get this
row.names x
1 1:5 0.41739378
2 6:10 0.96117176
3 11:15 -0.54342033
4 16:20 0.09633428
5 21:25 -0.07296177
6 26:30 0.60366540
7 31:35 -0.34679270
8 36:40 -0.07828379
9 41:45 0.89614252
10 46:50 0.65230839
but I would like to keep the row names of my matrix, to have something like this in the row.names:
row.names x
1 1966:1970 0.41739378
2 1971:1975 0.96117176
3 1976:1980 -0.54342033
4 1981:1985 0.09633428
5 1986:1990 -0.07296177
6 1991:1995 0.60366540
7 1996:2000 -0.34679270
8 2001:2005 -0.07828379
9 2006:2010 0.89614252
10 2011:2015 0.65230839
I have tried something like that, but doesn't work
running(test[,1], test[,2], fun=cor, width=5, by=5, rownames=rownames(test))
some direction?