0

I am very new to R and trying to manipulate my data in excel before moving it into R as a csv file. I would like to use ts() for weekly data. Can you do something as simple as Month 1-12, week 1-4, year? MWY. I was thinking if I use day 1-7 as week one and so on that would be uniform and work easy for my purpose but I don't know how to write it. Using this site and another for a tutorial I came up with this:

myts <- ts(Time2012, start = c(8/3/2013,1), end = c(9/2/2013,4), frequency = 52)

is there any easy way to denote the date to show I want to count weeks?

juba
  • 47,631
  • 14
  • 113
  • 118
Mark
  • 9
  • 1
  • 6
  • It is unclear what are you asking. You should give a sample(reproducible example) of your input and the desired output. – agstudy Sep 19 '13 at 19:19
  • In addition to providing a [reproducible and minimal example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) you should describe your question a bit better. What are you aiming to produce? Why a `ts` object rather than (say) a `zoo` object? Maybe a simple data frame would work better - more context please. – SlowLearner Sep 19 '13 at 19:38
  • Also please read [this](http://meta.stackoverflow.com/help/how-to-ask) and [this](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist). Thanks. – Henrik Sep 19 '13 at 20:06
  • In my crystal ball I see that you have a spreadsheet with three columns, Year, Month and Week. If so, I wish you all of Fortune's favours because R is terrible at interpreting dates with missing information (e.g. if you don't know at least the year and the day of the year). @SlowLearner's suggestions of `zoo` or a data frame might help. `zoo` understands irregular time series (e.g. week 1 doesn't follow 7 days after week 5), but if you need a `ts` object to feed to, say, `forecast`, it will be tricky. Try converting YMD into a pretend yyyy/mm/dd in Excel, then look at `lubridate`. – nacnudus Sep 19 '13 at 22:04

2 Answers2

2

i would recommend a slightly different workflow, one which you will likely find has broader utility:

> end = Sys.Date()
> start = end - 365

> class

> # create the index array comprised of date objects
> ndx = seq(start, end, by='weeks')
> class(ndx)
  [1] "Date"
> length(ndx)
  [1] 53

> # create a fake data array
> x = 1:length(ndx)
> mydata = sin(x/2)

> # import a time series library 
> require(xts)

> # create the time series
> myts = xts(mydata, order.by=ndx)

> myts[1:5]
               [,1]
  2012-09-19 3.479426
  2012-09-26 3.841471
  2012-10-03 3.997495
  2012-10-10 3.909297
  2012-10-17 3.598472

> class(myts)
  [1] "xts" "zoo"

> periodicity(myts)
  Weekly periodicity from 2012-09-19 to 2013-09-18 

Alternatively, if your data is not by week, then you can create a time series having a higher resolution (eg, days) then roll it up to weeks:

> ndx = seq(start, end, by='days')

> x = 1:length(ndx)
> mydata = sin(x/2) + 3
> myts = xts(mydata, order.by=ndx)

> myts[1:5]  
             [,1]
2012-09-19 3.479426
2012-09-20 3.841471
2012-09-21 3.997495
2012-09-22 3.909297
2012-09-23 3.598472

> periodicity(myts)
    Daily periodicity from 2012-09-19 to 2013-09-19 

> # now roll-up this daily series to weeks

> require(xts)

> # first create the endpoints
> np = endpoints(myts, on='weeks')


> myts_weeks = period.apply(x=myts, INDEX=np, FUN=sum, na.rm=TRUE)
> myts_weeks[1:5]
               [,1]
  2012-09-23 18.82616
  2012-09-30 17.11212
  2012-10-07 24.93492
  2012-10-14 17.51811
  2012-10-21 23.58635

> periodicity(myts_weeks)
  Weekly periodicity from 2012-09-23 to 2013-09-19 
doug
  • 69,080
  • 24
  • 165
  • 199
1

A simpler way to create a set of Year, Month, Week (of month) is with lubridate.

require(lubridate)

# Your starting date, plus 52 more dates at weekly intervals
xDates <- dmy("8/3/2013") + weeks(0:52)

# A data frame of the dates, the month of the year, and the week of the month
xYMW <- data.frame(date=(xDates), month=month(xDates), week=mday(xDates) %/% 7 + 1)
xYMW[1:5, ]
        date month week
1 2013-03-08     3    2
2 2013-03-15     3    3
3 2013-03-22     3    4
4 2013-03-29     3    5
5 2013-04-05     4    1
nacnudus
  • 6,328
  • 5
  • 33
  • 47