3 Replies - 351 Views - Last Post: 15 June 2018 - 09:40 AM Rate Topic: -----

#1 dano-IT   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 04-October 10

New to R programming need help with dates

Posted 15 June 2018 - 02:24 AM

Hello all its been long time since I have posted here :)
I have a question in R programming and hope I will get some help here
I have the following code :
FROM <- as.Date(from.date) #First date in my dataset which is 2015-01-01
  TO <- max(datafram$date[!is.na(dataframe$sales) & dataframe$sales != 0]) %m-% months(nback + 1) #nback is an integer specified by the user for how many month he wants to go back in this case its 6 so the value of TO should be 2017-10-01
  if(day(Sys.Date()) < 10){
    to_last <- max(dataframe$date[!is.na(dataframe$sales) & dataframe$sales != 0]) %m-% months(1) 
    } else {
      to_last <- max(dataframe$date[!is.na(dataframe$sales) & dataframe$sales != 0])
  }
  
  day(TO) <- day(to_last) <- 1


now my question how can duplicated the same code but for weeks
FROM <- as.Date(from.date) #First date in my dataset which is 2015-01-01
  TO <- max(datafram$date[!is.na(dataframe$sales) & dataframe$sales != 0]) %u-% weeks(nback + 1) #nback is an integer specified by the user for how many month he wants to go back in this case its 6 so the value of TO should be 2017-10-01
  if(day(Sys.Date()) < 10){
    to_last <- max(dataframe$date[!is.na(dataframe$sales) & dataframe$sales != 0]) %w-% weeks(1) 
    } else {
      to_last <- max(dataframe$date[!is.na(dataframe$sales) & dataframe$sales != 0])
  }
  
  day(TO) <- day(to_last) <- 1


I have tried %u %U %v also

Is This A Good Question/Topic? 0
  • +

Replies To: New to R programming need help with dates

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14690
  • View blog
  • Posts: 58,701
  • Joined: 12-June 08

Re: New to R programming need help with dates

Posted 15 June 2018 - 09:04 AM

Is it giving you an error? Not working as expected? Giving you a different result than expected?
Was This Post Helpful? 0
  • +
  • -

#3 dano-IT   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 04-October 10

Re: New to R programming need help with dates

Posted 15 June 2018 - 09:30 AM

it was giving me an error that this function don't exist. However, I have worked around and use strftime and its working now :)
Was This Post Helpful? 1
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14690
  • View blog
  • Posts: 58,701
  • Joined: 12-June 08

Re: New to R programming need help with dates

Posted 15 June 2018 - 09:40 AM

Rock on! Good to hear you solved it.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1