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
  • 5,377
  • 12
  • 50
  • 56
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
  • 233,926
  • 16
  • 195
  • 321