I have an excel with daily data for a variable (evaporation) for 5 places from 01/01/2000 to 08/31/2019 and I need to transform the information to quarterly data, this is the script I have:
library(readxl)
evaporacion <- read_excel("directorio",
col_types = c("date", "numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric"))
attach(evaporacion)
library(tseries)
data = ts(evaporacion,
start = c(2000,1,1),
end = c(2019,31,8),
frequency = 365)
mydata_quartely = aggregate.ts(data,nfrequency = 4 ,FUN = mean)
The problem arises when I execute the last line, an error appears saying that it cannot transform data from frequency 365 to 4.
Error in aggregate.ts(data, nfrequency = 4, FUN = mean) : cannot change frequency from 365 to 4
You can't, because 365 is simply not a multiple of 4. Each month, or four months, has a different number of days, depending on the month and even the year (leap). So you should always pivot with the exact date to be able to do any type of aggregation. With a
ts
you could make an aggregation if, for example, it was already monthly, to semesters, four-month periods, quarters and two-month periods because 12 is a multiple of all these periods.For what you are trying to do, if you already have one
data.frame
with onefecha
like the following:We can group by semester and apply the function
mean()
thanks toas.yearqtr()
the packagezoo
We already have the data organized by quarters in a
data.frame
common, if you want it in a time series, you could do: