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

Find average change in timeseries

I have an annual mean timeseries dataset for 15 years, and I am trying to find the average change/increase/decrease in this timeseries.

The timeseries I have is spatial (average values for each grid-cell/pixel, years repeat).

How can I do this in R via dplyr?

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

Sample data

year = c(2005, 2005, 2005, 2005, 2006, 2006, 2006, 2006, 2007, 2007, 2007, 2007, 2008, 2008, 2008, 2008)
Tmean = c(24, 24.5, 25.8,25, 24.8, 25, 23.5, 23.8, 24.8, 25, 25.2, 25.8, 25.3, 25.6, 25.2, 25)

Code

 library(tidyverse)

df = data.frame(year, Tmean)

    change = df$year %>%
      # Sort by year
      arrange(year) %>%
      mutate(Diff_change = Tmean - lag(Tmean), # Difference in Tmean between years
             Rate_percent = (Diff_change / year)/Tmean * 100) # Percent change # **returns inf values** 
    
    Average_change = mean(change$Rate_percent, na.rm = TRUE) 

>Solution :

We can put those vectors into a data frame to use with dplyr (…if that’s how you want to do it; this is straightforward with base R as well).

library(dplyr)
df <- data.frame(year, Tmean)
change <- df %>%
  arrange(year) %>%
  mutate(Diff_change = Tmean - lag(Tmean), # Difference in Tmean between years
         Diff_time = year - lag(year),
         Rate_percent = (Diff_change/Diff_time)/lag(Tmean) * 100) # Percent change 

Average_change = mean(change$Rate_percent, na.rm = TRUE)  

Results (with updated question data)

> change
   year Tmean Diff_change Rate_percent
1  2005  24.0          NA           NA
2  2005  24.5         0.5    2.0833333
3  2005  25.8         1.3    5.3061224
4  2005  25.0        -0.8   -3.1007752
5  2006  24.8        -0.2   -0.8000000
6  2006  25.0         0.2    0.8064516
7  2006  23.5        -1.5   -6.0000000
8  2006  23.8         0.3    1.2765957
9  2007  24.8         1.0    4.2016807
10 2007  25.0         0.2    0.8064516
11 2007  25.2         0.2    0.8000000
12 2007  25.8         0.6    2.3809524
13 2008  25.3        -0.5   -1.9379845
14 2008  25.6         0.3    1.1857708
15 2008  25.2        -0.4   -1.5625000
16 2008  25.0        -0.2   -0.7936508
> Average_change
[1] 0.3101632
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