3

I read this fantastic solution in this post. I would like to get two time series objects. One for the first day of each week and other for the last day of each week. My time series is irregular in the sense that doesn't include weekends and holidays. Weeks go from Monday to Sunday. This is the beggining of my series:

         Date   Val WeekNum
1  01/02/1990 41.38       1
2  02/02/1990 42.88       1
3  05/02/1990 42.00       2
4  06/02/1990 41.50       2
5  07/02/1990 42.25       2
6  08/02/1990 41.75       2
7  09/02/1990 42.13       2
8  12/02/1990 42.13       3
9  13/02/1990 42.00       3
10 14/02/1990 42.63       3
11 15/02/1990 43.75       3
12 16/02/1990 44.75       3
13 20/02/1990 44.13       4
14 21/02/1990 43.88       4
15 22/02/1990 44.38       4
16 23/02/1990 44.00       4
17 26/02/1990 44.00       5
18 27/02/1990 44.88       5
19 28/02/1990 44.50       5
20 01/03/1990 44.63       5
21 02/03/1990 46.00       5
22 05/03/1990 45.88       6
23 06/03/1990 45.50       6
24 07/03/1990 45.38       6
25 08/03/1990 43.63       6
26 09/03/1990 41.50       6
27 12/03/1990 41.38       7
28 13/03/1990 40.63       7
29 14/03/1990 40.25       7
30 15/03/1990 40.50       7
31 16/03/1990 40.50       7
32 19/03/1990 40.25       8
33 20/03/1990 39.88       8
34 21/03/1990 39.75       8
35 22/03/1990 38.50       8
36 23/03/1990 38.75       8
37 26/03/1990 39.63       9
38 27/03/1990 39.75       9

Then I use from the reference post:

library(xts)
do.call(rbind, lapply(split(x, "weeks"), function(x) x[1]))

EDIT: it wasn't working because I had the data as of zoo instead of xts.

Community
  • 1
  • 1
nopeva
  • 1,583
  • 5
  • 22
  • 38
  • 1
    What don't you understand? – Joshua Ulrich Apr 26 '13 at 10:54
  • @Joshua Ulrich I get just one value out of it but I expected to get as many as Mondays or Fridays are in the series. Also would that work in case that there is a missing Monday or Friday? – nopeva Apr 26 '13 at 11:02
  • You need to clarify what you want the result(s) to be. Do you want the first/last observations of each week, or do you want the Monday/Friday observations? – Joshua Ulrich Apr 26 '13 at 11:05
  • @Joshua Ulrich I want first the Monday/Friday of each week and, in case that any of them are a holiday then take Tuesday/Thursday and so on. I refer to a missing data when say a holiday. – nopeva Apr 26 '13 at 11:07

2 Answers2

4

You can simply use the first and last functions. You could also use head and tail.

# first/last
do.call(rbind, lapply(split(x, "weeks"), first))
do.call(rbind, lapply(split(x, "weeks"), last))
# head/tail
do.call(rbind, lapply(split(x, "weeks"), head, 1))
do.call(rbind, lapply(split(x, "weeks"), tail, 1))
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
2

You can use .indexwday to get day week number. Then using range you get the first and last one. Since , not all weeks begin with monday and finish with friday, it is better to take the min and max of each week.

To get the first and the last day of each week you can do something like this :

do.call(rbind, lapply(split(dat.xts, "weeks"), 
                      function(y) y[.indexwday(y) %in% range(.indexwday(y))]))



            Val WeekNum
1990-02-01 41.38       1
1990-02-02 42.88       1
1990-02-05 42.00       2
1990-02-09 42.13       2
1990-02-12 42.13       3
1990-02-16 44.75       3
1990-02-20 44.13       4
1990-02-23 44.00       4
1990-02-26 44.00       5
1990-03-02 46.00       5
1990-03-05 45.88       6
1990-03-09 41.50       6
1990-03-12 41.38       7
1990-03-16 40.50       7
1990-03-19 40.25       8
1990-03-23 38.75       8
1990-03-26 39.63       9
1990-03-27 39.75       9
agstudy
  • 119,832
  • 17
  • 199
  • 261