I have been using ts objects for a while but not too much xts, so i was trying to calculate mean, sum, last, first... (FUN) for every year in economic time series. When i try it i have weird result.
This is a quick example: Considering x and y like monthly series, for two consecutive years:
x <- 1:12
y <- 1:12
serie <- c(x,y)
Then i create a ts object
serie <- ts(serie,1990,,12)
now i convert it to xts object
library(xts)
serie.xts <- as.xts(serie)
plot(serie.xts)
as far i see, the series is properly created
serie.xts
The series looks ok
ene 1990 1
feb 1990 2
mar 1990 3
abr 1990 4
may 1990 5
jun 1990 6
jul 1990 7
ago 1990 8
sep 1990 9
oct 1990 10
nov 1990 11
dic 1990 12
ene 1991 1
feb 1991 2
mar 1991 3
abr 1991 4
may 1991 5
jun 1991 6
jul 1991 7
ago 1991 8
sep 1991 9
oct 1991 10
nov 1991 11
dic 1991 12
Then comes my problem
for example, i try to get the last value for every year, as far i know this is what this function do:
apply.yearly(serie.xts,FUN = "last")
but i get this:
ene 1990 1
ene 1991 1
dic 1991 12
I was expecting this: dic 1990 12 dic 1991 12
What am i doing wrong? Is this the way this function works? What this function do actually?
i don't understand what is this result. Please help. And thanks for reading my question.
R 3.3.3 xts 0.9-7 zoo 1.8-0
period.apply(INDEX = endpoints(x = serie.xts, on = "years"), x = serie.xts, FUN = "last")- Victor EspinozayearmontoDatelike thisindex(serie.xts) <- as.Date(index(serie.xts))- sechstein