0

This is probably a very basic question I have downloaded a Google trends file and the date range for each row is a week format similar to 2004-01-04 - 2004-01-10. This is basically Sun to Sat

Pseudo Code here where the Week is equal to the format above (2004-01-04 - 2004-01-10)

googledata <- googledata %>%
  mutate(startdate = ymd(substr(Week, 1, 10)),
     enddate = ymd(substr(Week, 14, 26)),
     weekno = isoweek(startdate),
     weekno2 = isoweek(enddate))

Sample Data

Week                    Vol
2004-01-04 - 2004-01-10 15
2004-01-11 - 2004-01-17 12
2004-01-18 - 2004-01-24 10
2004-01-25 - 2004-01-31 10
2004-02-01 - 2004-02-07 9
2004-02-08 - 2004-02-14 9

I have two questions

Is this a specific week type for example ISO. Is it possible to convert it to a week number using lubridate or some other function in R. The problem is using isoweek and week result in split weeks for the start and end date

I have a second data set i want to match up but it uses a different Week Number type, so i would like to convert them both to the Google type

Thank you for your help

John Smith
  • 2,448
  • 7
  • 54
  • 78
  • `as.yearmon()` and then specifying what you want (e.g. month (`%b`) or year (`%Y`) , etc...) should do what you want. – Edu Jun 23 '16 at 13:19
  • Hi @Edu, thanks for the feedback but im not sure i follow. I need it on the as a week number – John Smith Jun 23 '16 at 13:22
  • It would be good if you can provide a sample of your data... – Edu Jun 23 '16 at 13:23

1 Answers1

0

This is addressed here

You have to transform your date into POSIXlt object and then apply strftime specifying the week %W argument. In your sample I would create two different variables corresponding the initial date and final date for each week. It may be easier for manipulations.

    Initial      Final
1 04/01/2004 10/01/2004
2 11/01/2004 17/01/2004
3 18/01/2004 24/01/2004
4 25/01/2004 31/01/2004
5 01/02/2004 07/02/2004
6 08/02/2004 14/02/2004

Another alternative could be to create a variable with the number of weeks (example$n_week = rep(1:6))

Community
  • 1
  • 1
Edu
  • 903
  • 6
  • 17
  • Hi @Edu, I saw and tried this but the problem is that google trends week is count Sun to Sat as a week. So when i use this method it doesnt give me a whole week. The start date is in the first week and the end date is in the next week – John Smith Jun 23 '16 at 14:17