creating list of ts objects based on list of dataframes in R

穿精又带淫゛_ 提交于 2020-04-17 22:49:35

问题


So I had previously posted a similar question but this is kind of an extension to it. The complicacy of my data set has increased and now my data set is a list of data frames where each data frame has a KEY, CAL, OAS. KEY is the unique value which changes from one data frame to another. CAL is the timescale I have to utilize to make a ts object expressed in week.year. and OAS is the number of units that are recorded for every week. The original data set has some 250 data frames, for convenience I am posting just 2.

   KEY      CAL      OAS     
  444-12235140 01.2019 144.5667  
  444-12235140 02.2019 139.6333  
  444-12235140 03.2019 212.6667  
  444-12235140 04.2019 415.7000  
  444-12235140 05.2019 433.5333  
  444-12235140 06.2019 439.8000 

dataframe 1

     KEY       CAL      OAS 
556-11337513 21.2019   0.00000 
556-11337513 22.2019   0.00000 
556-11337513 23.2019   0.00000 
556-11337513 24.2019  57.00000 
556-11337513 25.2019  17.20909 
556-11337513 26.2019 130.01818 

dataframe 2

The CAL ranges from 1 to 52 (with values missing for some weeks) and year changes as well from 2019 to 2021. I have tried altering the approach (as suggested by a previous generous contributor):

 tt <-list() 
 for (i in 1:num_of_unique_keys) {  
 week <-as.integer(ds2[[i]][[2]]) 
 year <- as.numeric(sub("...", "",ds2[[i]][[2]])) 
 zz <- zoo(ds2[[i]][[3]], year + (week - 1) / 52)  
  tt[[i]] <- ts(zz, frequency = 52) 
 }

num_of_unique_keys is the number of dataframes. But I am facing the error

Error in seq.default(head(tt, 1), tail(tt, 1), deltat) : 'from' must be a finite number

Any ideas as to how it can be resolved?

来源:https://stackoverflow.com/questions/60549725/creating-list-of-ts-objects-based-on-list-of-dataframes-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!