# How to use monthly data to calculate quarterly average

**URL:** <https://discourse.julialang.org/t/how-to-use-monthly-data-to-calculate-quarterly-average/94709>\
**Category:** Finance and Economics\
**Tags:** question\
**Created:** [February 16, 2023, 5:21am UTC](https://discourse.julialang.org/t/how-to-use-monthly-data-to-calculate-quarterly-average/94709 "2023-02-16T05:21:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![WhY](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/why/32/46541_2.png) [@WhY](https://discourse.julialang.org/u/WhY)\
**Post date:** [February 16, 2023, 5:21am UTC](https://discourse.julialang.org/t/how-to-use-monthly-data-to-calculate-quarterly-average/94709/1 "2023-02-16T05:21:57Z")

</div>

I am trying to use a monthly data to calculate the quarter average, suppose now I randomly generate 10000 month, and try to use this to get the quarterly average by using the loop:

```julia
T = 10000
a=rand(Int,T) # monthly data
u=zeros(Int64((p.T - p.T %3) /3)) # indicate size of quartly data
for i ∈ 1:3:(T - T %3)
    u[i]=mean(a[i:i+3])
end 

```

I understand that the problem is the i, since the size for u and a is different. But I don’t know how tofix this

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [February 16, 2023, 8:47am UTC](https://discourse.julialang.org/t/how-to-use-monthly-data-to-calculate-quarterly-average/94709/2 "2023-02-16T08:47:59Z")

</div>

Is this what you’re looking for?

```julia
T = 10000
a = rand(Int,T) # monthly data
u = zeros(T ÷ 3) # indicate size of quartly data
j = 1
for i ∈ 1:3:T-3
    u[j] = mean(a[i:i+3])
    j += 1
end 

```

I use integer division `÷` (`\div<TAB>`) and introduce the iteration index `j` into `u` for simplicity

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 16, 2023, 9:35am UTC](https://discourse.julialang.org/t/how-to-use-monthly-data-to-calculate-quarterly-average/94709/3 "2023-02-16T09:35:06Z")

</div>

Regarding previous post, there are possibly some typos in the loop: `1:3:(T-2)` and in `mean(a[i:i+2])`?

Alternative one-liner:

```julia
[mean(view(a, i:min(i+2,T))) for i in 1:3:T] 

```

---

<div class="post-metadata">

**Author:** ![piechologist](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piechologist/32/27662_2.png) [@piechologist](https://discourse.julialang.org/u/piechologist)\
**Post date:** [February 16, 2023, 9:44am UTC](https://discourse.julialang.org/t/how-to-use-monthly-data-to-calculate-quarterly-average/94709/4 "2023-02-16T09:44:32Z")

</div>

You’re dropping the last value (at index 10000). We’ve got 3334 quarters. Assuming that the last quarter contained only one month we could do:

```julia
T = 10000
a = rand(Int, T)
u = zeros(ceil(Int, T/3))  
a1 = [a; fill(last(a), 3-T%3)] # complete the last, partial quarter with the last value
for (j, i) ∈ enumerate(1:3:T)
    u[j] = mean(a1[i:i+2])
end

```

Working with date and time can be messy. How about a missing month? How about our data start in the middle of a quarter? I would really make proper time stamps of type `Date` and handle the quarters with the `Dates` STDLIB. Also, `TimeSeries.jl` offers a `collapse` function that can handle quarterly data (see [Combine methods · TimeSeries.jl](https://juliastats.org/TimeSeries.jl/stable/combine/#collapse-1)).

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [February 16, 2023, 10:10am UTC](https://discourse.julialang.org/t/how-to-use-monthly-data-to-calculate-quarterly-average/94709/5 "2023-02-16T10:10:33Z")

</div>

The easiest in this case is to use the `partition` function. It also properly handles the last part, that is shorter than three elements.

```julia
julia> map(mean, Iterators.partition(a, 3))
3334-element Vector{Float64}:
<...>

```

---

<div class="post-metadata">

**Author:** ![piechologist](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piechologist/32/27662_2.png) [@piechologist](https://discourse.julialang.org/u/piechologist)\
**Post date:** [February 16, 2023, 10:55am UTC](https://discourse.julialang.org/t/how-to-use-monthly-data-to-calculate-quarterly-average/94709/6 "2023-02-16T10:55:40Z")

</div>

Here an example with `TimeSeries.jl`. The data may start and end with partial quarters:

```julia
using Dates, Statistics, TimeSeries
T = 10000
d = firstdayofmonth(today())
timestamps = d-Month(T-1):Month(1):d # range with the latest 10000 months
ta = TimeArray(timestamps, rand(Int, T))
u = collapse(ta, Dates.quarter, first, mean)

```

---

<div class="post-metadata">

**Author:** ![mdogan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdogan/32/47244_2.png) [@mdogan](https://discourse.julialang.org/u/mdogan)\
**Post date:** [February 23, 2023, 9:55pm UTC](https://discourse.julialang.org/t/how-to-use-monthly-data-to-calculate-quarterly-average/94709/7 "2023-02-23T21:55:36Z")

</div>

You may want to use [Tsframes](https://xkdr.github.io/TSFrames.jl/stable/) package - you can easily change the frequencies of time series.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 24, 2023, 6:03am UTC](https://discourse.julialang.org/t/how-to-use-monthly-data-to-calculate-quarterly-average/94709/8 "2023-02-24T06:03:57Z")

</div>

> [@WhY](#):
>
> 10000 month

Wow. That is a very old organization. Are you reviewing the finances of the Vatican?

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [February 24, 2023, 11:21am UTC](https://discourse.julialang.org/t/how-to-use-monthly-data-to-calculate-quarterly-average/94709/9 "2023-02-24T11:21:38Z")

</div>

```julia
vec(mean(reshape(@view(a[1:T-(T % 3)]), (3, :)); dims=1))

```

is efficient on my machine.
