Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Averaging my data into quarterly means (nfrequency error message)

I’m trying to average my data into quarterly means but when i use the following code i get this error

code

quarterly = aggregate(overturning_ts, nfrequency = 4, mean)

error message

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Error in aggregate.ts(overturning_ts, nfrequency = 4, mean) : 
cannot change frequency from 1 to 4

date snippet

overturning_ts
    year month day hour Quarter Days_since_start Overturning_Strength
[1,] 2004     4   2    0       2              1.0             9.689933
[2,] 2004     4   2   12       2              1.5            10.193495
[3,] 2004     4   3    0       2              2.0            10.660849
[4,] 2004     4   3   12       2              2.5            11.077229
[5,] 2004     4   4    0       2              3.0            11.432414
[6,] 2004     4   4   12       2              3.5            11.721769

all data available here, after downloading i just converted it to a time series to get overturned_ts: https://drive.google.com/file/d/1NV3aKsvpPkGatLnuUMbvLpxhcYs_gdM-/view?usp=sharing

outcome i am looking for here;

   Qtr1   Qtr2   Qtr3   Qtr4
1960  160.1  129.7   84.8  120.1
1961  160.1  124.9   84.8  116.9
1962  169.7  140.9   89.7  123.3

>Solution :

Like this?

library(tidyverse)

df %>%  
  group_by(year, Quarter) %>% 
  summarise(avg_overturning = mean(Overturning_Strength, na.rm = TRUE)) %>%  
  pivot_wider(names_from = Quarter, 
              values_from = avg_overturning, names_sort = TRUE)
    
# A tibble: 11 x 5
# Groups:   year [11]
    year   `1`   `2`   `3`   `4`
   <dbl> <dbl> <dbl> <dbl> <dbl>
 1  2004 NA     15.3  23.7  17.7
 2  2005 14.0   18.7  21.7  22.5
 3  2006 17.1   17.7  20.5  20.8
 4  2007 18.9   15.5  17.9  20.0
 5  2008 18.5   15.5  16.1  20.2
 6  2009 16.3   14.9  15.3  12.2
 7  2010  8.89  16.2  19.7  15.1
 8  2011 15.1   16.0  17.8  18.4
 9  2012 15.8   11.9  16.4  16.5
10  2013 11.9   17.1  17.6  18.8
11  2014 15.1   NA    NA    NA  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading