Convert week number to date

后端 未结 4 612
离开以前
离开以前 2020-11-28 11:30

I have a data frame in R with the week of the year that I would like to convert to a date. I know I have to pick a year and a day of the week so I am fixing those values at

4条回答
  •  无人及你
    2020-11-28 11:45

    It will be like using 2nd year = (week-52), 3rd year  = (week -104)...so on
    
    for(i in 1:456548)
    {
      if (train[i,2] > 0 & train[i,2] <53)
      {
        train["weekdate"] <- as.Date(paste(2016, train$week, 1, sep="-"), "%Y-%U-%u")
      }
      if (train[i,2] > 52 & train[i,2] <105)
      {
        train["weekdate"] <- as.Date(paste(2017, (train$week-52), 1, sep="-"), "%Y-%U-%u")
      }
      if (train[i,2] > 104 & train[i,2] <150)
      {
        train["weekdate"] <- as.Date(paste(2018, (train$week-104), 1, sep="-"), "%Y-%U-%u")
      }
    
    }
    

提交回复
热议问题