Search code examples
rtime-seriestimeseriescharttrend

Create time series in R with weekly measurements for 30 years period


I have a set of weekly data for 30 years (1991 - 2020). The data was collected weekly between 5th may - 10 October every year. This gives me 23 weeks of data every year for 30 years.

I want to create a time series in R with this data. How do I do that please? It should be just 690 entriesin the output, but it is generating 1531 entries in the output See my codes and data below:

I saw a similar question HERE, but mine repeats for 30 years.

myts <- ts(df$Kc_Kamble,  start = c(1991, 1), end = c(2020, 23),  frequency = 52)

Output in R:
Time Series:
Start = c(1991, 1) 
End = c(2020, 23) 
Frequency = 52  

Sample data:

  Year  Week    Kc_Kamble
1991    1   0.357445197
1991    2   0.36902168
1991    3   0.383675947
1991    4   0.400703221
1991    5   0.418901921
1991    6   0.437049406
1991    7   0.453742803
1991    8   0.467291036
1991    9   0.475942834
1991    10  0.476898402
1991    11  0.464632341
1991    12  0.436298927
1991    13  0.396338825
1991    14  0.352731819
1991    15  0.313539638
1991    16  0.283932169
1991    17  0.2627343
1991    18  0.247373874
1991    19  0.235647483
1991    20  0.225655859
1991    21  0.216663659
1991    22  0.208550065
1991    23  0.203605036
1992    1   0.336754943
1992    2   0.334735193
1992    3   0.342654691
1992    4   0.363520428
1992    5   0.397733301
1992    6   0.4399758
1992    7   0.483592219
1992    8   0.521920773
1992    9   0.548597061
1992    10  0.560150059
1992    11  0.557210705
1992    12  0.542114151
1992    13  0.5173071
1992    14  0.485236257
1992    15  0.448348321
1992    16  0.409089999
1992    17  0.369907993
1992    18  0.333162073
1992    19  0.300014261
1992    20  0.270225988
1992    21  0.243406301
1992    22  0.219247646
1992    23  0.204966601

Solution

  • Let me suggest the following steps to set up and start analyzing your time series.

    1. Initialize your time series by creating a 'dates' sequence and 'data' (set to NA). Use the library xts to create the time series.
      library(xts)
      dates <- seq(as.Date("1991-01-01"), as.Date("2020-01-01"), by = "weeks")
      data  <- rep(NA, length(dates))
      myxts <- xts(x = data, order.by = dates)
      str(myxts); head(myxts); tail(myxts)
    
    1. Collect your data. Data is collected weekly between 5th may - 10 October every year. Let's read the data and work with Weekly Total Precipitation for year 2014.
      ts_data <- read.table("https://www.dropbox.com/s/k2cxpja3cpsyoyc/ts_data.txt?dl=1", header =TRUE, sep="\t") 
      year.2014 <- ts_data[which(ts_data$Year == 2014),] 
      year.2014   # 23 rows of data for 2014.
    
      start <- as.Date("2014-5-5"); end <- as.Date("2014-10-10")
      collect <- which ( index(myxts) >= start & index(myxts) <= end )
      myxts[collect] <- year.2014$PRPtot 
    
      # year.2014 and collect must have the same number of rows
    
    1. Verify the collected data. You should see data inside each time window, and NA outside the time windows.
      myxts2 <- window(myxts, start=start-50, end=end+50)
      str(myxts2);  myxts2
    
    1. Visualize the collected data. You could view the complete time series (i.e. myxts). Note that autoplot drops all NAs.
      library(ggplot2)
      autoplot(myxts2, geom = "point")