0

I am having some data with a time column expressed in week.year and a corresponding unit that was measured in that week.

    Week-Year             Units
    01.2020             39.12727273
    02.2020             33.34545455
    03.2020             118.7181818
    04.2020             83.71818182
    05.2020             58.56985
    .                    .
    52.2020             89.54651534

I have to create a ts object which takes these Week-Year values as input. The reason for requiring this step is- there are sometimes values missing for certain weeks so using an auto generated time scale (start=, end=, frequency=) will mess up the readings. Is there any way of achieving it? or is there any way to accommodate such a situation? R novice here, would really appreciate some guidance. :)

Ivar
  • 6,138
  • 12
  • 49
  • 61
charu1313
  • 29
  • 6

1 Answers1

0

Assuming the input is the data frame DF shown reproducibly in the Note at the end, convert it to a zoo object and then use as.ts to create a ts series with frequency 52.

library(zoo)

week <- as.integer(DF[[1]])
year <- as.numeric(sub("...", "", DF[[1]]))
z <- zoo(DF[[2]], year + (week - 1) / 52)
tt <- as.ts(z)

tt
## Time Series:
## Start = c(2020, 1) 
## End = c(2020, 52) 
## Frequency = 52 
##  [1]  39.12727  33.34545 118.71818  83.71818  58.56985        NA        NA
##  [8]        NA        NA        NA        NA        NA        NA        NA
## [15]        NA        NA        NA        NA        NA        NA        NA
## [22]        NA        NA        NA        NA        NA        NA        NA
## [29]        NA        NA        NA        NA        NA        NA        NA
## [36]        NA        NA        NA        NA        NA        NA        NA
## [43]        NA        NA        NA        NA        NA        NA        NA
## [50]        NA        NA  89.54652

frequency(tt)
## [1] 52

class(tt)
## [1] "ts"

Note

Lines <- "    Week-Year             Units
    01.2020             39.12727273
    02.2020             33.34545455
    03.2020             118.7181818
    04.2020             83.71818182
    05.2020             58.56985
    52.2020             89.54651534"
DF <- read.table(text = Lines, header = TRUE, colClasses = c("character", NA))
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • the input is in fact a data frame. And thanks :) it works :) – charu1313 Feb 25 '20 at 12:18
  • facing a subsequent error since my dataset has changed.I am now dealing with a list of dataframes & need to create a list of ts objs such that each ts obj corresponds to a dataframe(i.e. first ts obj for first data frame & so on.) I tried tweaking the solution provided above: tt <-list() for (i in num_of_unique_keys) { week <-ds2[[i]][[1]] year <- as.numeric(sub("...", "", ds2[[i]][[1]])) z <- zoo(ds2[[i]][[2]], year + (week - 1) / 52) tt[i] <- as.ts(z) } i am getting error- Error in seq.default(head(tt, 1), tail(tt, 1), deltat) : 'from' must be a finite number – charu1313 Mar 05 '20 at 14:46
  • Need a reproducible example. – G. Grothendieck Mar 05 '20 at 15:34
  • https://stackoverflow.com/questions/60549725/creating-list-of-ts-objects-based-on-list-of-dataframes-in-r the updated query. – charu1313 Mar 05 '20 at 16:33
  • Hey I figured out what my problem is- For those dataframes where CAL.YEAR do not start with the first week i.e. 1.xxxx It gives periodicity error. I think what needs to be tweaked is this- "year + (week - 1) / 52)". Can you please explain the logic behind this. – charu1313 Mar 06 '20 at 16:07
  • I don't think so. If I remove the first row of DF in the Note at the end it still works correctly. – G. Grothendieck Mar 06 '20 at 16:13
  • It works well if the WEEK.YEAR starts with the first week i.e. 01.2020 but if it starts wit something like 15.2019 or 26.2020 it is giving me the periodicity error – charu1313 Mar 09 '20 at 08:06
  • I can't reproduce that. If I run `DF <- structure(list(Week.Year = c("15.2020", "16.2020", "17.2020", "18.2020", "19.2020", "20.2020"), Units = c(39.12727273, 33.34545455, 118.7181818, 83.71818182, 58.56985, 89.54651534)), class = "data.frame", row.names = c(NA, -6L))` and then run the code in the answer there are no errors. – G. Grothendieck Mar 09 '20 at 08:38
  • is there a way to share my dataset? – charu1313 Mar 09 '20 at 09:10
  • Try to cut it down while keeping the problem. – G. Grothendieck Mar 09 '20 at 09:15